Read the statement by Michael Teeuw here.
pm2 doesnt work with crontab
-
Hello,
Im trying to restart my MM every day in the night so i want to use a crontab.
Might be easy:
sudo crontab -eand add
59 23 * * * pm2 restart mmBut that doesnt work.
The crontab seems to work:Feb 27 13:37:01 raspberrypi CRON[3581]: (root) CMD (pm2 restart mm)And i did a pm2 flush before and after the crontab did its job there is nothing in the log:
pi@raspberrypi:~ $ pm2 logs --lines=100 [TAILING] Tailing last 100 lines for [all] processes (change the value with --lines option) /home/pi/.pm2/pm2.log last 100 lines: /home/pi/.pm2/logs/mm-error.log last 100 lines: /home/pi/.pm2/logs/mm-out.log last 100 lines: 0|mm | [27.02.2021 13:38.28.339] [INFO] Newsfeed-Fetcher: Broadcasting 39 items.so it seems that pm2 didnt do anything.
Next try after some online research:
pi@raspberrypi:~ $ which node /home/pi/.config/nvm/versions/node/v10.23.0/bin/node pi@raspberrypi:~ $ which pm2 /home/pi/.config/nvm/versions/node/v10.23.0/bin/pm2i changed the crontab to
1 14 * * * /home/pi/.config/nvm/versions/node/v10.23.0/bin/node /home/pi/.config/nvm/versions/node/v10.23.0/bin/pm2 restart mm(1 14 is for testing)
It still doesnt work: no output in pm2 log.
Does anyone have an idea?
Thanks :)
-
@keineahnung123 u need to run pm2 as the user that did the save and where mm was running. it cannot be root

you also create a user specific crontab in /var/spool/cron
user needs to own the file, then username is not required on the contab entries themselvesEach user, including root, can have a cron file. These files don't exist by default, but can be created in the /var/spool/cron directory using the crontab -e command that's also -
@sdetweil Thanks.
With
sudo nano /etc/crontaband
30 16 * * * pi /home/pi/.config/nvm/versions/node/v10.23.0/bin/node /home/pi/.config/nvm/versions/node/v10.23.0/bin/pm2 stop mmit seems to work :)
-
@keineahnung123 i don’t think u need those paths now
-
45 16 * * * pi pm2 stop mmdoesnt work. I have to set the paths.
I think there went something wrong when i upgraded to buster.
I had a shellscript withcd ~/MagicMirror && npm startwhich worked fine and after the upgrade to buster i have to use
cd ~/MagicMirror && /home/pi/.config/nvm/versions/node/v10.23.0/bin/npm startotherwise it says “npm: not found”
Same seems to be with crontab…
-
@keineahnung123 looks like u need to reinstall nvm
-
@sdetweil said in pm2 doesnt work with crontab:
@keineahnung123 looks like u need to reinstall nvm
nvm or npm?
-
@keineahnung123 nvm
-
@keineahnung123
Hello, i Can send you a script om monday, where you make backup to a small .img and where is restart you MagicMirror on the same time. -
@Assassins
I would like to see this script. Please. -
This is all I use in my crontab -e
15 07 * * * pm2 start mm
30 21 * * * pm2 stop all
Shuts MM down at 9:30 pm
Starts MM backup at 7:15 am
EVERYDAY.
I have a Black background color and the bar at the bottom using black background which makes it look like the screen is off basically.
That way it doesn’t flood the room with light at night.
Also less read / writes to the ssd drive I use for my MM. -
@sdetweil
My old crontab does not work in debian 12 aka bookworm
Is there something i need to add to it to get it to work again?
Really liked how it worked and the fact that I was getting a fresh restart of mm every morning…15 07 * * * pm2 start mm 30 21 * * * pm2 stop allTried this too.
15 07 * * * pi pm2 start mm 30 21 * * * pi pm2 stop all -
@plainbroke what userid did the pm2 jobs get created under?
-
@sdetweil
pi is my user. I know not very original or safe but it is on a closed network.
that is why I tried the pi user in the crontab.
and it shows up when I do crontab -l.
So I think it is setup under pi correctly.
Do I need to do it as root sudo crontab -e and use pi or root as the user? -
@plainbroke no. what fo the pm2 logs show?
pm2 logs --lines=xxx
xxx is number of most recent lines, default 15 -
you can do cron restart with pm2 too, see https://pm2.keymetrics.io/docs/usage/restart-strategies/
-
This post is deleted! -
@plainbroke i mean after cron trying to start
pm2set a cron entry for an hour from now stop MagicMirror and flush the logs
pm2 stop all
pm2 flushsee what the logs say then
-
@karsten13 yes, but he wants stop and start
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login