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
Related
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.
I wrote a Python script which backs up mongoDB, and it works fine when I test run directly in terminal.
However, I get an error from cron saying mongodump: command not found - although the command mongodump works fine when I run the script directly in terminal.
Contents of crontab -e:
* * * * * cd <path-to-script> && python3 script.py
After looking into the post provided by S3DEV's.
Running the full env path of mongodump into the python script worked.
To get the full path of mongodump, in terminal:
which mongodump
>>/usr/local/bin/mongodump
In my case i am using os.system() in my script.
os.system(/usr/local/bin/mongodump [commands])
instead of
os.system(mongodump [commands])
This is because programs started from cron don't get the environment your login shell uses. In particular, PATH is usually quite minimal. The tried and tested way to run scripts from cron is:
Always use an absolute path to a script in the crontab, say /path/to/script.
The beginning of /path/to/script sets and exports PATH and any other variables needed, e.g. with export PATH=$(/usr/bin/getconf PATH):/usr/local/bin
You can test whether any script would run with a reduced environment with
env -i HOME=$HOME /path/to/script
If that runs ok, it is ready for cron.
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 am using dryscrape in a python script. The python script is called in a bash script, which is run by cron. For those who may not be aware, dryscrape is a headless browser (use QtWebkit in the background - so requires an xsession).
Here are the main points concerning the issue I'm having
When I run the python script from the command line, it works
When I run the bash script from the command line, it works too
I figured out that this may have something to do with different environments between my command prompt and when the cron job is running, so I modified my bash script to source my .profile as follows:
#/bin/bash
. /full/path/to/my/home/directory/.profile
python script_to_run.py
This is what my cronjob crontab entry looks like:
0,55 14-22 * * 1-5 /path/to/script.sh >> $(date "+/path/to/logs/\%Y\%m\%d.mydownload.log" )
By the way, I know that the job is being run (I can see entries in /var/log/syslog, and the script also writes to a log file - which is where I get the error message below):
In all cases, I got the following error message:
Could not connect to X server. Try calling dryscrape.start_xvfb()
before creating a session
I have installed the prerequisites, on my machine (obviously - since it runs at the command line). At the moment, I have run out of ideas.
What is causing the script to run fine at the console, and then fail when run by cron?
[[Relevant Details]]
OS: Linux 16.0.4 LTS
bash: version 4.3.46(1)
cron user: myself (i.e. same user at the command prompt)
dryscrape: version 1.0.1
The solution to this was to call the dryscrape.start_xvfb() method before starting the dryscrape session.
Cron user does not have display, so you cannot run any command which requires a display.
You need to modify the python script to do not use any type of display (check carefully, because some python commands, even though they do not open any display , they internally check for this variable).
The best way to test is to ssh into the machine without Display, and check if you can run it from there without erros.