Crontab not working

March 3, 2024, 20:19

danielw2909

Hi, I have never worked with cronjobs before and I need a little help. I have a Pi 5 that hosts a small website using a python script. Now I want the Pi to reboot every day at 5 AM and automatically start the server. I searched for ways to do that and I came around crontab According to a guide I created a new job sudo crontab -e I added the following lines: @reboot root sleep 60 && sudo python /mnt/ssd/server.py > ~/logs/server.log 2>&1 0 5 root sudo reboot I added the sleep to make sure the ssd with the server on it is mounted by the time the job is executed However after rebooting nothing happened. So for debugging I added: root echo &date >> ~/logs/minute.log This should write the current time and date to a minute.log file But after waiting for a couple of minutes still nothing happened I have no idea what I missed and why it is not working. Does anyone have suggestions what I can try to make it work?

oops.se

When you edit crontab and add sudo before crontab it is roots crontab you are changing. So change crontab to
0 5   * reboot

oops.se

And I would advice you create a bash file with @reboot root sleep 60 && sudo python /mnt/ssd/server.py > ~/logs/server.log 2>&1 and I also suspect that sudo is making the life harder for you.

danielw2909

ok, I've tried that

danielw2909

now I have the following lines
0 5    reboot
@reboot sleep 60 && python /mnt/ssd/server.py > ~/logs/server.log 2>&1
     echo &date >> ~/logs/minute.log

danielw2909

but that also doesn't do anything

oops.se

Oh it could be your environment variables, the bash shell that crontab uses has NO env variables! so reboot should be /usr/sbin/reboot

oops.se

And the echo &date use just /usr/bin/date >> /home/<user>/logs/minute.log

oops.se

And I dont think ~ works,use absolute paths

danielw2909

Now I have this
@reboot /usr/bin/sleep 60 && /usr/bin/python /mnt/ssd/server.py > /home/daedalus/logs/server.log 2>&1
0 5    /usr/sbin/reboot
     /usr/bin/date >> home/daedalus/logs/minute.log

oops.se

You have missed a / before home

danielw2909

All right the minute.log works now

danielw2909

I'll thy the reboot now

oops.se

Nice to see that you know how to troubleshoot, you simplified the problem and thats makes it easier to isolate the issue

danielw2909

thanks

oops.se

YW

oops.se

Did it work?

danielw2909

Kind of. The command did run but I got a No such file or directory error

danielw2909

now I'm working on that

danielw2909

the server.py was not found

oops.se

The mount happen after crontab run ? Then you should look into how to make a systemd unit file of server.py

danielw2909

Ok, but I'm new to Linux and I don't know what systemd unit files are

danielw2909

I've got it working now. I just had a typo in the path to server.py

oops.se

Nice

oops.se

And systemd is the service that starts other services in the background. And in the unit file you can describe dependencies, example if you want a web server it should have a dependency "network" because what use do a web server have without a wworking network. Check: https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/ and https://linuxhandbook.com/create-systemd-services/ if you are interested to learn more

danielw2909

cool, thanks