Run Python Script Using Cygwin Crontab on Windows - python

I succeed in setting up Cygwin Crontab on Windows. I need to run Python script every 5 minutes.
*/5 * * * * run-one C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
Above didn't work in crotab. Also the log file cygstart cron.log is empty. How I'm suppose to fix and run the script?
P.S. Script is running fine using idle editor.

Run your script directly in the Cygwin command line:
C:\Python27\Scripts\myScript.py > C:\Python27\Scripts\myScript.log 2>&1
If it does not work, you may need to specify python.exe directly and use forward slash ("/") rather than backslash ("\"):
C:/Python27/python.exe C:/Python27/Scripts/myScript.py > C:/Python27/Scripts/myScript.log 2>&1
If it works, then please make sure cron is running. If it is running, it should give similar output as below:
$ cygcheck.exe -c | grep cron
cron 4.1-61 OK
cron-debuginfo 4.1-61 OK
$ cygrunsrv -Q cron
Service : cron
Display name : Cron daemon
Current State : Running
Controls Accepted : Stop
Command : /usr/sbin/cron -n
$ ps -lef | grep cron
SYSTEM 4852 4680 ? 15:16:50 /usr/sbin/cron
If it is not running, run
$ cygrunsrv --start cron
If there is an error, you may need to reinstall cron.
$ cyglsa-config
Then
$ cron-config
And follow the instructions.

Related

problem with cronjob to run python script on ubuntu server

I have installed python3 following the Digital Ocean guide and to run my python scripts just by command line, this is the bash I use and it works:
~$ source /home/username/python_projects/project_name/bin/activate
~$ python3 /home/username/python_projects/project_name.py
~$ deactivate
if I put that commands in crontab in the same order, nothing happens.
0 7 * * * source /home/username/python_projects/project_name/bin/activate
0 7 * * * python3 /home/username/python_projects/project_name.py
0 7 * * * deactivate
What am I doing wrong?
Cronjob is active and running.
~$ systemctl status cron
The python file has this permissions:
-rw-rw-r-- 1 user_name user_name 17075 Feb 7 02:30 python_projects/project_name.py
The activate job runs, then exits, and then the next cron job knows nothing about what it did. You want to run them all in the same process (though running deactivate at the end is pointless, as everything that activate did will be wiped when the job ends anyway).
In practice, you can run the Python interpreter from within the virtual environment directly.
For what it's worth, the error message about "no MTA installed" means that the output from cron was discarded because the system could not figure out how to send it by email. You'll probably want to change the rule to write the output to a file.
0 7 * * * cd python_projects && ./project_name/bin/python3 project_name.py >>project_name.log 2>&1
Perhaps notice that cron always starts in your home directory, so you don't have to spell out /home/username (provided of course that username is the user whose crontab this runs from).
There are a few things that could be causing your cronjob not to run:
Environmental variables: The source command in your cronjob sets up the virtual environment, but this environment is not passed on to the cron process. You should specify the full path to the Python executable in your virtual environment, e.g. /home/username/python_projects/project_name/bin/python3.
Permission issues: Ensure that the cron user has permission to execute the script and access the virtual environment.
Output: By default, cron does not send any output to the terminal. If your script produces output, you may want to redirect it to a file for debugging purposes. You can do this by adding > /tmp/output.log 2>&1 to the end of your crontab entry.
You can check the system logs for any error messages related to your cron job. The logs are usually located in /var/log/syslog or /var/log/messages.
I hope this helps!

Why doesn't my cronjob execute as expected?

I edit the crontab in Ubuntu 18.04:
crontab -e
50 21 * * * /data/min/query/sync.sh >> logs/tag.log
Then I saved it and exited. I did this at 21:41pm, and I suppose it will execute it at 21:50pm, but it didn't effect. I checked the logs/tag.log, but nothing occurs.
The content of my sync.sh:
#!/bin/bash
cd /data/min/query
echo 'sync tags now ...'
source venv/bin/activate
python utils/tag_counter.py
echo 'updating resource files are done!!!'
I make the script executable by:
chmod +x sync.sh
So that sync.sh can be executed directly. The script does work when executed at the command line, but not working in the cronjobs. This is the first time I use cronjob. Is there anything I am missing in setting up the cronjob?

crontab won't run os.system python command

Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)

Running crontab with python

Python crontab script doesnt seem to work. When i run it manually,
python /home/ec2-user/code1.py
it works fine but when put into cron.txt file for the crontab, doesnt.
My crontab file is:
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
i also tried
0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1
But neither have much luck.
sudo crontab -l
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
Shows everything functional.
I tried Crontab not running my python script and couple others with not much luck either.
EDIT:
With
PATH=/opt/python2.7/bin
MAILTO=my#email
*/5 * * * * /home/ec2-user/code1.py
Email i get is:
/bin/sh: /home/ec2-user/code1.py : No such file or directory
Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.
Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
and still no luck
Verify cron is running: ps aux | grep [c]ron should show a running cron process
Remove the redirects from the command so that cron emails you the output
Add a MAILTO=<email address> to your crontab, so that you get the email
Put the full path to python (/opt/python2.7/bin/python) instead of just python in the command
Add another command to crontab such as echo FOOBAR and verify that you get the email.
ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
Run crontab -l | cat -A so that we can verify all the control characters are correct.
did you check the following points?
is your script executable? chmod 700 code1.py
the first line in your code should be, in most cases the python is installed at this place
#!/usr/bin/python
after that the crontab as follow should execute
0 * * * * /home/ec2-user/code1.py >/dev/null 2>&1
If the error message is correctly copy/pasted, it seems to reveal that there is a problem with the crontab file. If you created it on a foreign platform, it might be best to start over with an empty file, this time creating it in a native editor.
As others have already pointed out, redirecting output and errors to /dev/null basically makes debugging impossible, so don't do that. If your program creates copiously verbose uninformative output, run it in a wrapper which filters out the trivial diagnostics, or, if it is your own program, rewrite it to run silently in normal operation.
Did you try "/usr/bin/python" instead of "python"?
ps ax | grep python
will give you the path you could use.
try this command that should hopefully where your python is :
which python
very likely you will have something like
/usr/bin/python /home/ec2-user/code1.py

Add CRON via shell to run Python

My cron doesn't seem to execute every 5 mins. Can anyone show me where I have gone wrong?
I made it executable using this command:
chmod +x /etc/utilities/poll.py
I can run it manually with this command:
cd /etc/utilities
python poll.py
When I run it like this I get an error:
root#li453-78:~# /etc/utilities/poll.py
-bash: /etc/utilities/poll.py: Permission denied
This is the command I use to add it to shell (via my automatic deployment script):
crontab -l | { cat; echo "*/5 * * * * /etc/utilities/poll.py"; } | crontab -
The start of my python file is like this:
#!/usr/bin/env python
So, could someone please enlighten me about how I should be adding the cron to my debian server via shell so that it executes?
Using the help from here, for whatever reason, even though I had the code correct to make the script executable, this line didn't seem to fire in my deployment script, meaning that all I had to do was run it afterwards to make it executable and then everything worked.
Lesson learnt: If you need to do this, the code above works

Categories

Resources