In crontab I have:
#reboot /usr/bin/python3 /root/mydirectory/mypythonfile.py
Using: which python3 I get: /usr/bin/python3
at the start of the python script I have: #!/usr/bin/python3
The python file is executable for everyone.
Running ./mypythonfile.py in shell works o.k.
I have no idea left what to do, to make this file execute via crontab. I played in crontab and executed some commands (not python script) and it worked fine.
I am logged in as Root, so this should not be a problem as well.
EDIT:
How do I know it's not running?
I created a script, which sends e-mail, inserts dummy data in database and I checked Htop for .py file. Zero on all.
Related
I am trying to schedule python script through crontab. Functionality of python script is svn cleanup and update commands. When i manually try python script, it is working as expected. Same script scheduled through crontab it is working but not getting proper result (result will be same as of last python script output, i.e when i manually run python script that will be output eventhough there are checkin's)
Below is python script:
subprocess.call("/usr/bin/svn cleanup "+path,shell=True)
subprocess.call("/usr/bin/svn update "+path,shell=True)
subprocess.call("/usr/bin/svn log --username usrname --password
pass1 "+path+">filename.txt",shell=True)
Below is command which is used in crontab,
/usr/bin/python /path_to_script/filename.py
Please help me on this.
Thanks
I'm trying to run a python script on my raspberrypi using cron.
I did the following:
crontab -e # To edit a crontab job
After the cron file opened, I added the following line:
#reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!
If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.
What's strange:
I checked the log file and it's empty, so it seems like everything goes fine
If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log
I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!
The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:
#reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log
Some other possible reasons it might not work:
working directory issue (if you're using relative paths)
a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
the script does not output anything (would need some more details about the script)
Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1
I have a python script (script A) that runs multiple sub-processes (running another python script - script B). When I run the script manually, it works fine, but when it is being run using crontab it behaves differently.
To be more specific, my script is limited to 100TB of quota, so script B runs over a tree and produce a text file of size-per-dir. This script is being run multiple times on several dir-trees as a subprocess, and the results are being further analyzed by script A.
When I run manually, both scripts run fine. But when I run it using Cron, script A runs fine but script B produce odd resutls.
Any ideas of why using Cron would affect a python script?
Make sure the crontab you are using is with full path of the .sh file.
And in beginning of your script try to add the following:
. /home/gemapp/.bash_profile
to load all needed items in bash_profile.
The default directory for any scheduled job is user's home dir, not the location of the shell script/program itself.
To mimic the crontab job, you must simulate running your script from user's home dir using "cd ~" command. Run your job using the exact same syntax as in your "crontab -l" output to simulate the error.
In other word, you must simulate running your schedule job from home dir. Once it worked, then you put it into the crontab.
I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.
I want to run a python script at boot of Lubuntu 15.04. This python script writes some string into a text file placed in /home/aUser/aFile.txt
My /etc/rc.local file is:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
And the script /home/aUser/theScript.py is:
#!/usr/bin/python
f = open('/home/aUser/aFile.txt','w');
f.write("Some string...");
f.close();
Actually the python script does more, and run an infinite loop, this is why I run the script in background with &. Of course I have python installed:
~$ python --version
Python 2.7.9
I checked if /etc/rc.local is called at boot, and it is, proof of that: I added a test into the /etc/rc.local in this way:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
echo "Test" >> /home/aUser/aTest.txt
and the file /home/aUser/aTest.txt is written/created at boot.
So everything looks correct, proof of that:
if I run manually ~$ /etc/rc.local the file aFile.txt is correctly written.
Instead if I start (or reboot) the OS, the file is not written at boot.
I suspect that could be a problem of permissions/user: I know that /etc/rc.local is run as root, but even if I set root or aUser as owner of the file, the situation is the same. Also run the python script in the /etc/rc.local as user aUser (with su command) does not solve the problem.
Ok I found the problem and fix it, thanks to the #Zac comment.
Actually the python script try to open a network connection before writing the file: at boot time, when the python script is run from /etc/rc.local (so, it is run), the network is still not ready (probably because it is a wireless network) and therefore an exception is raised and the entire script stops. Capturing the exception solves the problem.
So at the end it was my fault, (not) helped by the rc.local that does not provide an easy way to debug.