Back

How to set up cron job in railway app

Laravel task schedule requires cronjob to be set up for it to work properly. However, I was not able to implement cronjob in the railway app. They do have documentation on implementing the cron in a different service using node-cron here . My requirement was to implement the cron in the same server as the laravel app and was unable to do it initially. I tried installing cron package as well but with no luck. I think it is still better to have cron as a separate service in terms of microservices but if you have a requirement to run the cron in the same laravel server, then you can try this approach. Head to this link if you want to watch the video instead of reading. I also have a post to deploy Laravel app in railway here

We're going to use the method suggested by railway app but run it in the service that you want.

Firstly, install the node packages 

npm install node-cron
npm install shelljs

 Create a file in root directory cron.ts

const cron = require('node-cron');
const shell = require('shelljs');

// Schedule tasks to be run on the server.
cron.schedule('* * * * *', function() {
  console.log('Running cronjobs');
  shell.exec('php artisan schedule:run >> /dev/null 2>&1');
});

Now we need to run this file in the service that you want but it needs to be run at the time of deployment. This was the part where i was stuck the most because nixpack builds and railway.toml had a place to add the command but it overrides the default commands and hence did not work.

You'll need to look into the start of the build logs of a successful deployment and see build plan. Copy the code present against start and format it in an editor.

Mine looked something like this

([ -e /app/storage ] && chmod -R ugo+w /app/storage); perl /assets/transform-config.pl /assets/nginx.template.conf /nginx.conf && echo "Server starting on port $PORT" && (php-fpm -y /assets/php-fpm.conf & nginx -c /nginx.conf)

and the code to run the nodejs file in background is

node /app/cron.ts&

We now just need to merge those two like below

(node /app/cron.ts&) && ([ -e /app/storage ] && chmod -R ugo+w /app/storage); perl /assets/transform-config.pl /assets/nginx.template.conf /nginx.conf && echo "Server starting on port $PORT" && (php-fpm -y /assets/php-fpm.conf & nginx -c /nginx.conf)

This code then needs to be placed in Start Command under Deploy in Railway Settings tab. You can also use railway.toml file to do this. More info here. Once you do this the service will deploy itself again and you can see that "Running cronjobs" is printed in deployment logs every minute.

Sometimes, the build might fail when you put all the above code from the UI in Start Command. So better to put in a shell script

Create a file in the root directory start.sh and copy the command from above into it.

(node /app/cron.ts&) && ([ -e /app/storage ] && chmod -R ugo+w /app/storage); perl /assets/transform-config.pl /assets/nginx.template.conf /nginx.conf && echo "Server starting on port $PORT" && (php-fpm -y /assets/php-fpm.conf & nginx -c /nginx.conf)

Now the code that needs to be placed in Start Command in settings tab is sh /app/start.sh

railway appcroncronjobphplaravelnodejsnixpackshelljsnode-cron

Latest Post

Information retrieval – Searching of text using Elasticsearch

Information retrieval is the process of obtaining relevant information resources from the collection of resources relevant to information need.

Learn more
© 2023 www.lamadly.com