Restart pm2 app every 12h with cron - node.js

Tried this, but it doesn't work:
SHELL=/bin/bash
PATH=/usr/lib/node_modules/pm2/bin
* 0,12 * * * pm2 restart all
What am I doing wrong?

Edit cron with crontab -e and add the following:
0 */12 * * * /usr/bin/node /usr/bin/pm2 restart all
For the schedule, use 0 */12 * * * for every 12 hours, or 0 0,12 * * * for 0:00 and 12:00 specifically. (Your schedule, * 0,12 * * *, would trigger every minute of hour 0 and hour 12, 0:00, 0:01, 0:02...)
For the command, as fedorqui mentioned, use the path to node followed by the path to pm2 and the pm2 options. Use which node and which pm2 to get the path to node and pm2.

Among multiple restart strategies, PM2 can restart application based on a cron format via the option --cron-restart
Restart app every midnight:
pm2 start app.js --cron-restart="0 0 * * *"
For more information check out the doc:
https://pm2.keymetrics.io/docs/usage/restart-strategies/#restart-at-cron-time

Don't restart, reload (zero down-time)
Reload myApp every day at 4:30 AM
30 4 * * * /usr/local/bin/node /usr/local/bin/pm2 reload <myAppId> > /dev/null 2>&1
To check the full path of node and pm2 do which node and which pm2. The portion > /dev/null 2>&1 ignores the stdout and stderr.

Related

Run yarn script into crontab

I made a script in TypeScript that download data from some api and store inside a mongo DB.
If i run yarn start from the app folder it works well.
I would like to put this command in a cron job that will be executed every 5 minutes.
I try it with some sintax in crontab but ti doesn't work.
I try to put the call in a run.sh script but it doesn't work too.
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts
*/5 * * * * cd /opt/app-folder && /usr/bin/yarn start > /home/username/app-name-out.txt
*/5 * * * * /home/username/run.sh > /home/username/app-name-out.txt
*/5 * * * * /home/username/.nvm/versions/node/v16.15.1/bin/ts-node /opt/app-folder/src/main.ts > /home/username/app-name-out.txt
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts > /home/username/app-name-out.txt
Can someone help me to execute the main.ts every 5 minutes?
Thanks
I get rid of this problem.
There was 2 problems, the first related to the output redirection.
I fixed by redirect stdout in a file and stderr in another one.
The second was related the the $PATH of crontab: it was /usr/bin:/bin.
To fix it I log into my user where script works and I print my $PATH with echo $PATH.
I copied the value and I set it before the crontab line in crontab file.
This is what it looks like:
# Set the same path of user username to have the correct path in script
PATH=/home/username/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
# Execute oracle every 5 minutes
*/5 * * * * /bin/sh /home/username/run.sh >> /home/username/app-name-info.txt 2>> /home/username/app-name-error.txt
Now it works.

Centos7 Crontab jobs is not executing periodically

Am using a centos7 VPS server, recently I noticed that my website Crontab is not executing periodically on it own as scheduled. I have list of cron jobs but not of it will execute when the time come, but if I login to my CWP web panel and manually click run it will execute my command successfully.
Please how do I fix my Crontab issue?
0 0,12 * * * rm -rf /home/www/temp/*
* * * * * /usr/local/bin/php /home/www/public_html/debug/cron.php >> /home/www/public_html/debug/_exec.log 2
In php cron.php
<?php
echo "Received Debugging Request";
?>
Well, as I think, the crontab that is already configured should be changed to something like this:
1 0,12 * * * rm -rf /home/www/temp/*
Why is that?
Because the current configuration 0 0,12 * * * rm -rf /home/www/temp/* means:
At minute 0 past hour 0 and 12.
is probably avoiding 00:00 and 12:00 because the condition says that it must execute after 00 and 12, it should be executed at 00:01 and 12:01.

Cron won't execute none of my commands on Ubuntu 21.10 impish

I'm trying to run a Docker container every other minute that is stopped via cron job but it seems not working.
What I've done is launch the command crontab -e and add the line
*/1 * * * * docker start sender >> /home/cronlog.log 2>&1
I've added the user group to Docker as explained here (in fact I can access docker from the terminal without sudo)
I have also tried to add the command into a script as below
*/1 * * * * /home/start_container.sh >> /home/cronlog.log 2>&1
with the script containing
#!/bin/sh
docker start sender
but still, nothing happens. The cron process is working tho as using the command ps -ef | grep cron I got
root 881 1 0 08:42 ? 00:00:00 /usr/sbin/cron -f -P
nicola 10905 10178 0 11:31 pts/0 00:00:00 grep --color=auto cron
Am I missing something? (Obviously, the commands work if launched manually from the terminal)
Try using the docker path instead.
type the following command to get the path of docker.
$ where docker
/usr/bin/docker
/bin/docker
then try any one of the paths in the cron script
*/1 * * * * /bin/docker start sender >> /home/cronlog.log 2>&1
or
*/1 * * * * /usr/bin/docker start sender >> /home/cronlog.log 2>&1
It turned out that, for some reason, the cron doesn't like the /home/ (at least, in this specific instance)
I've fixed using another path such as
*/1 * * * * docker start sender >> /tmp/cronlog.log 2>&1

How you call the restart service

how can I execute this command with crontab
0 * * * * service ogp_agent restart
It doesn't work for me, what should I do?

Can't make crontab work

I am new to Linux and Ubuntu and I seldom have to use it. I am trying to make this PHP script to run every minute using cron, but firstly I wanted to make some tests.
I created an empty file at /var/www/html/ called test. I ran on terminal:
sudo crontab -e
And added this line:
0 * * * * rm /var/www/html/test
Then saved it and exited. It said "Installing new Crontab"
Nothing happened. Then I created a file bfile.sh that contained:
#!/bin/sh
rm /var/www/html/test
and added the following to crontab:
0 * * * * bash /var/www/html/bfile.sh
Still nothing happened.
What do I have to do to see anything happening from crontab? By the way I checked and the service is running
0 * * * * basically says "run this at 0th minute of every hour."
If you need cron to run your command every minute do * * * * *.
0 * * * * runs once every 1 hour. If you want to run every minute it should be */1 * * * *
You can also check the /var/log/cron file for any errors

Resources