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.
Related
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.
I am facing an issue of crontab does not run the python script in Raspbian GNU/Linux 9.4 (stretch) installed on a Raspberry Pi3. I have done lots of research on this topic e.g. by following the troubleshooting guide in https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527 , but none of them solved my issue.
The python script that I want to run is located in
/home/pi/Documents/Fork_BookManager.py
I have made sure that anyone can execute, read and change content to the above file.
I can run this file through terminal by writing
usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
I know the file is running, because the Fork_BookManager.py is using selenium to open a web browser, and I see this web browser being opened. There is no error observed when running the Fork_BookManager.py file from terminal.
In terminal executed
crontab -e
Using nano as editor, I first verified that crontab acutally worked by
* * * * * env > /home/pi/Documents/env.output
After a minute there is a env.output file in Documents folder with below parameters.
LANGUAGE=da_DK.UTF-8
HOME=/home/pi
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=da_DK.UTF-8
SHELL=/bin/sh
LC_ALL=da_DK.UTF-8
PWD=/home/pi
I noticed that the path from crontab is different from the path when env is called from terminal as I will have the below path if I call env from terminal
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
So I know that crontab -e was working since I otherwise would not have got the env.output file created in Documents folder. I proceed by calling crontab -e from terminal and copied the path from env into the crontab like below
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
* * * * * /usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
Saved the script and waited. Nothing happend.
To summerize I have:
Made sure that the Fork_BookManager.py is executable
Verified that crontab is working
Updated path from env into the crontab script
which are three largest cause of experiencing difficulties in making crontab to work according to this guide
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527
Still it is not working. What have I missed? Is there a better practice to achieve what I want, namely to execute the python script once a minute without doing it directly from the python script itself such as a while loop with a time.sleep(60)
ADDITIONAL INFORMATION
I even tried to expand the path in crontab script by
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/bin:/home/pi/Documents
in an attempt to specify in which folder the python3 and Fork_BookManager.py is located. Still no luck.
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.
This question already has answers here:
UBUNTU: XOpenDisplay(NULL) fails when program run in boot sequence via rc.local
(2 answers)
Closed 7 years ago.
I have to run a bash script on bootup which opens couple of terminals and runs some command in each terminal.
test.sh (My bash file)
#!/bin/bash
sleep 10
gnome-terminal --tab-with-profile="Default" -e 'bash -c '\''export TAB=1; mkdir /home/naman/Desktop/test_folder'\'
I have created a testjob.conf inside /etc/init/ :
testjob.conf (Upstart config file)
description "A test job file for experimenting with Upstart"
author "Naman Kumar"
start on runlevel [2345]
exec echo Test Job ran at `date` >> /var/log/testjob.log
exec bash /home/naman/Desktop/test.sh
Now, the problem is testjob.conf is not able to run the test.sh file on bootup (or it runs it but does not create a folder test_folder). If I remove the last line from testjob.conf : exec bash /home/naman/Desktop/test.sh, everything works and when I do cat /var/log/testjob.log, I get the correct output but if the last line is there, cat /var/log/testjob.log does not give the latest output.
I have also tried updating /etc/rc.local with : bash /home/naman/Desktop/test.sh but that also does not seem to be running the test.sh script on bootup
I am not sure whether they run the scripts but are not able to create the folder or they are not even able to run the script on bootup.
Note: I can not use System -> Preferences -> Startup Applications because I don't have any monitor, so the desktop application does not run. (I am running it on a Single Board Computer with no monitor).
Does anyone know what is the issue here and why is the test.sh script not running properly on bootup?
Thanks in advance.
Naman
Cong ma is pretty close on this. Sysvinit is for system level daemons. It has no idea for users. That means it doesnt have a way to interact with your window system (or gnome).
Further: without a monitor, what would you expect gnome-terminal to do? gnome-terminal would open up a terminal on the monitor: which you can't see.
What you should look at is taking your commands (date, etc) and putting them in /etc/rc.local and not trying to 'olay them into a different terminal or anything. Just literally run the commands there.
I made a shell script and registered to execute every 20 minutes.
Here is my crontab code.
*/20 * * * * sh /mypath/run_myprocess.sh &> /dev/pts/34
I editted code like this in order to see whether my process run correctly.
I get the result '/dev/pts/34' from tty command in terminal.
However, does anyone know how to use linux command results(in this case: /dev/pts/34)
in crontab? This is because I will use several terminal to run my tasks.
For example, in shell script, I can use linux command result in the form of $(command) such as
echo "$(date)"
directly.
Plus, if I type something on the terminal during process running with crontab, it actually gives result. For example,
Process is running........
ls
backup backup.sh Desktop Task_Folder shared_folder
[UserID] ~ #
So I guess cron jobs run correctly but in background.
Please help me to find out how can I bring cron jobs in foreground.
If you start a job on your console and background it you can then bring it to foreground. If the task is not yours or not started on your terminal then you can not.