I am struggling since a couple of days to make a python script run whenever the pi reboots using crontab. I have used quiet a number of solutions but the job doesn't run at reboot.
I have tried
#reboot python3 /home/pi/Desktop/my_test.py.
I have also tried a few other fixes like giving full path of python, delaying the start of the file but nothing seems to work. Is there a bug or am I missing something?
Please help me out guys
Not a direct answer to the question, but in the end I added my script in /etc/rc.local instead of cron. I think there are too many bugs in cron.
With the following:
chmod +x /etc/rc.local
chmod 755 /etc/rc.local
Related
I was very hesitant to post here since this question have been popped out a ton, but I've tried pretty much everything I've found on the internet in last 2 days. I am on my first week using Linux and its been a wild ride. (Ubuntu 20.04 LTS)
So I made node app which opens browser -> logins to our company webapp and writes down my work hours automatically, I want to run it on computer reboot since I mark my hours when I get home. This way I dont forget to mark them. (note: I have also tried running it on every minute, or the next coming minute just to be sure its not about #reboot command)
These are some of the different options I've tried. Cant really remember all since I've been trying, I belive over 100 different variants now. Also on the codes below, I've also tried with either full paths or just ex. bin/node etc.
#reboot cd /home/sepi/Documents/MyProjects/eas_app && /usr/local/bin/node index.js
#reboot usr/local/bin/node /home/sepi/Documents/MyProjects/eas_app/index.js
#reboot /bin/node /home/sepi/Documents/MyProjects/eas_app/index.js
which node gives: /usr/local/bin/node
First check where is your node binary by
$ whereis node
and use that path only in cronjob.
To resolve any cronjob first thing you need to do is to redirect stdout and stderr in a log file.
#reboot /bin/node /home/sepi/Documents/MyProjects/eas_app/index.js > out.log 2>&1
This way you will understand if is there any library or path issue.
If you are still facing the issue then just add the below lines in your crontab
SHELL=/bin/bash
BASH_ENV="/home/user/.bashrc"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
In BASH_ENV instead of user add your username, check by whoami
Note: SHELL and PATH entry can be found by echo $SHELL and echo $PATH
respectively.
Also first add time base cron to test if it is working then add cronjob for reboot scenarios.
Hello I need help trying to figure out how to run a shell script in startup. I know that the magicmirror.sh script works and I know it is in the right path. When I reboot my raspberry pi it does not run the script. I have added the line of code to the end of the crontab using sudo crontab -e. The code is as follows.
#reboot sleep 60 && /home/pi/magicmirror.sh
The problem could be the way you call commands in your script.
As the crontab runs from e very minimal shell, not all environment vars get loaded.
So maybe the $PATH var is missing / not fully loaded, so some binary you call from inside the script is not found.
I guess you script starts with #!/bin/bash. If my guess is correct either try to start te script with #!/bin/env /bin/bash (this means "start bash with a full environment") or replace all calls/commands in you script with their full path. e.g. /usr/local/bin/myprog dosomething instead of myprog dosomething.
EDIT:
As #shv mentioned this could ALSO be a problem of permissions. But maybe in a different kind of way. If you run sudo crontab -e you are editing the crontab of root, not of your pi user. This has 2 effects:
you script is run in the environment of root. Sometimes some configurations enable you to do things differently than the root user. I am by no means a Raspberry pi expert, but it could be that your magic mirror accesses some GPIO pins. I can imagine that, although having more permissions to GPIO, root has to interact in a different way with GPIO than your pi user. (wild guess)
This is actually a security risk. Your pi user can edit the magicmirror.sh at will and write anything it it. So for example, someone having access to that pi user (either because he "hacked" the system, or is just someone you gave access to it) could write e.g. rm -rf --no-preserve-root / and just rebooting the device, to clean the filesystem. And you would not want to to that, do you?
To fix this you can either just edit you own crontab with crontab -e (without sudo) or put that script somewhere only root can access (if you need the root permissions) e.g. /root/magicmirror.sh
I think it's an issue with permissions.
try use crontab -e without sudo.
On my raspberry pi, I have entered the following entry in my crontab using crontab -e (without sudo):
* * * * * echo "Hello World" &>> /home/pi/test.txt
Just as a test, that is.
After restarting cron (sudo /etc/init.d/cron restart) the test.txt file is created in /home/pi/, but the content remains empty.
Why?
If I run this without the asterisks in my ssh terminal on the Rasberry Pi, it works fine.
I got to this point because my goal is to run a python script and log any errors, because sometimes it stops running and I don't know why. Hence my need for logging.
Thank you for your help!
P.S. I have the same problem on my Orange Pi that runs Raspbian (Debian Buster), but there cron doesn't work for some reason so there I am using rc.local to run my scripts on boot. But the same problem arises: Log file is created but no content is added.
It appears that Raspbian is using dash as the default shell for cron. dash is sh compliant and not bash compliant. That means that you cannot use &>>. You must use >> output.log 2>&1 instead.
You're saying that your goal is to run a Python script. There's a catch with that too. Python's buffering output so if it's too short it won't appear in your log file by default. You can use python -u for unbuffered output.
I hope that my answer makes sense.
I wan to run a python script every time my computer boots up. For this I have seen quiet some tutorials that suggest to do
crontab -e
#reboot python3 /home/user/Desktop/my_script.py
It would be nice if I can also start is after lets say 30 seconds, so I do
#reboot sleep 30; python3 /home/user/Desktop/my_script.py
I have tried it on my computer as well as on a Raspberry Pi, but it doesn't seem to work. To check I make the python script make a graph and save it on desktop, but it doesn't do so. However , it runs fine if I run it manually from terminal. Can someone help what I am missing here.
Thanks
Can you try using /etc/rc.local file ?
Add the command inside the file with full path (example: /usr/bin/python ...) and mark the file executable.
chmod +x /etc/rc.local
Note: Make sure to use full path to the executables, else export PATH variable in the /etc/rc.local file.
I have the following bash script to update to my website my current ip. It works fine stand alone, but put into a startup file, fails upon startup. I'm guessing it's a sequencing thing, but I'm not sure how to fix the sequencing, and after a few hours of googling and trying everything I can think of, I'm hoping someone can lead me in the right direction! This is what I am trying to run:
#!/bin/sh
IP_ADDR=$(ifconfig eth0 | sed -rn 's/^.*inet addr:(([0-9]+\.){3}[0-9]+).*$/\1/p')
wget -q -O /dev/null http://example.com/private/RPi_IP.php?send=${IP_ADDR}
I can't figure out what to do. I tried adding it to other startup programs even, and it fails upon startup too. I'm using a Raspberry Pi. Any ideas?
Your path might not be what you expect. You should fully-qualify any commands that you use. Especially for programs that live in /sbin/
ie
/sbin/ifconfig
/usr/bin/sed
/usr/bin/wget