Why doesn't Crontab run the command inserted by python script? - python

I am trying to modify the crontab of the root user through a script of python (main.py).
This script will open the crontab for root user and will modify it inserting a new command to call single.py with a parameter. The command used is not the problem as I have run it from the command line and it works. When I run the main.py script I can see the new entry when I run the crontab -l command. Everything looks correct but it does not work when is the time (nothing has been executed).
Doing some checks, if I execute the crontab -e command and I insert the command manually; when I save and close it; it shows: **crontab: installing new crontab** and then it works.
main.py:
fname = "/var/spool/cron/crontabs/root"
f = open(fname,"w+")
f.write("24 19 16 01 * python /home/pi/single.py 2087111972\n")
f.close() # save and close cron file
BTW: As this script has to be recursive (it will call itself for rescheduling its execution by modifying the crontab), is there anyway to apply the changes on crontab through the script?

Related

Running a TimesTen query from a Python script works fine. When I put it in my crontab, it doesn't work as expected

I have a python script which queries a TimesTen database and stores the output in a file, and emails it off. When I run it from its directory /home/directory/script.py, it works absolutely fine and pulls the data from the TimesTen db to a text file as instructed.
However, when I put it in cron, the python script still runs and doesn't error. The issue is that the output files are blank, which isn't the case when run normally.
The crontab entry is as follows:
05 14 * * * /usr/bin/python /export/home/user/script/bin/script.py
Does TimesTen db not run with cron or am I missing something very obvious? Do I need to put the absolute path of TimesTen when running the command? Such as:
os.system('/path/to/TimesTen/ttIsqlCS -f file.txt dsn=dsn > output.txt')
At the moment in my python script I run the TimesTen query as:
os.system('ttIsqlCS -f file.txt dsn=dsn > output.txt')
Any help would be appreciated.
Thanks in advance.

Cronjob not running on Python and Mac

I am trying to test a simple cronjob and when I check if the cronjob has executed, it shows that it has but no file is being created as part of the task.
I have the following script, testjob.py which needs to be executed:
#!/usr/bin/env python3
import datetime
with open('testcron.txt', 'a') as outfile:
outfile.write('\n' + str(datetime.datetime.now() + 'myname'))
This is the cronjob:
#!/usr/bin/env python3
from crontab import CronTab
my_cron = CronTab(user='myname')
job = my_cron.new(command = 'python /Users/myname/Desktop/customers/cronjob/testjob.py')
#schedule job to run every 2 minutes
job.minute.every(1)
my_cron.write()
How can I troubleshoot this?
link to image with my crontab running via crontab -e: https://i.stack.imgur.com/pj9Pt.png
You can set up a cron job via crontab -e, which is much better than creating a python script to create a cron job. But anyways.
You can first troubleshoot by actually running your python script and seeing if there are any errors, and if the file is even created.
make sure the user for that cron job has the right permissions to execute the script.
try executing the command: python /Users/myname/Desktop/customers/cronjob/testjob.py directly from your terminal.
Judging from your response, the reason why its not working is because your script isn't able to open the text file.
When you're executing the script via: python /Users/myname/Desktop/customers/cronjob/testjob.py, the location of your text file "testcron.txt" depends entirely on where you are executing the script from.
So basically, unless "testcron.txt" is located in the same path / directory from where you are executing the script, its not going to work.
You can fix this by changing your cron tab to first navigate to where your text file is, and then run the python script.
For example, if your "testcron.txt" file is located in /Users/myname/Desktop/customers/cronjob/ then write your cron job as:
cd /Users/myname/Desktop/customers/cronjob && python ./testjob.py
You can instead of running the cron job with the python command run it like a shell script.
testjob.py:
#!/usr/bin/env python3
import datetime
with open('testcron.txt', 'a') as outfile:
outfile.write('\n' + str(datetime.datetime.now() + 'myname'))
Cronjob:
#!/usr/bin/env python3
from crontab import CronTab
my_cron = CronTab(user='myname')
job = my_cron.new(command = '/Users/myname/Desktop/customers/cronjob/testjob.py')
#schedule job to run every 2 minutes
job.minute.every(1)
my_cron.write()
Make sure you run chmod a+x /Users/myname/Desktop/customers/cronjob/testjob.py first to make the python executable.

Crontab in Python creates cron job, but doesn't run script

Not sure what I'm doing wrong whether it's the code, the directory, or something else. Please help!
from crontab import CronTab
my_cron = CronTab(user='bgoldberg')
job = my_cron.new(command='python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py')
job.minute.every(1)
my_cron.write()
And here's the writeDate.py script:
import datetime
with open('dateInfo.txt','a') as outFile:
outFile.write('\n' + str(datetime.datetime.now()))
The writeDate.py script just writes the current timestamp to a txt file and it works fine when run separately. When I run python scheduleCron.py, it runs without error but it seems that it's not running the writeDate.py script because no txt file is created. When I enter crontab -l it correctly shows the job that was created: ***** python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py
Not sure what I'm doing wrong...
This is a cron "gotcha". Cron uses the command
python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py
which you expect to write to your current working directory, but cron will write to /var/log/syslog or some variation of this by default. It is trying to write to some place you don't have the permissions to (but it won't die), so you need to specifiy the absolute path of your output file.
changing your line in writeDate.py to write to an absolute path:
with open('/Users/bgoldberg/dateinfo.txt', 'a') as outFile:
will solve your problem.

Crontab - Python Script Runs, sends output to mail, but doesn't write to file

I have a job that I have set to run every 5 minutes. I confirm that it indeed runs by running:
grep CRON /var/log/syslog
It shows that it is running the exact command I need it to run 'python3 /root/foofolder/foo.py R'
Dec 29 23:05:01 fooserver CRON[9306]: (root) CMD (python3 /root/foofolder/foo.py R)
It sends the output to mail. I read the mail and output looks as if I was running it in terminal, exactly as I need. The script is supposed to write out to a file. It fails to do so. Running the script in the command line writes the file just fine. I've given the .py file +x permissions and added
#!/usr/bin/python
to the beginning of the .py script. Is there anything else I am missing?
Try using absolute paths and/or os.chdir to a well-known location early in your script (before opening the file).

Crontab fails to execute Python script

crontab fails to execute a Python script. The command line I am using to run the Python script is ok.
These are solutions I had tried:
add #!/usr/bin/env python at the top of the main.py
add PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin at the top of crontab
chmod 777 to the main.py file
service cron restart
my crontab is:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
*/1 * * * * python /home/python_prj/main.py
and the log in /var/log/syslog is:
Nov 6 07:08:01 localhost CRON[28146]: (root) CMD (python /home/python_prj/main.py)
and nothing else.
The main.py script calls some methods from other modules under python_prj, does that matter?
Anyone can help me?
The main.py script calls some methods from other modules under python_prj, does that matter?
Yes, it does. All modules need to be findable at run time. You can accomplish this in several ways, but the most appropriate might be to set the PYTHONPATH variable in your crontab.
You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks.
[update] here is the top of my crontab:
www:~# crontab -l
DJANGO_SETTINGS_MODULE=djangocron.settings
PATH=...
PYTHONPATH=/home/django
MAILTO="cron-notices#example.com"
...
# m h dom mon dow command
10-50/10 * * * * /home/django/cleanup_actions.py
...
(running cleanup actions every 10 minutes, except at the top of the hour).
Any file access in your scripts? And if so, have you used relative paths (or even: no explicit path) in your script?
When run from commandline, the actual folder is 'your path', where you start the script from. When run by cron, 'your path' may be different depending on environment variables.
So try using absolute paths to any files you access.
Check the permissions of the script. Make sure that it's executable by cron-- try chmod +x main.py.

Categories