I installed CroneTab, open the editor using crontab -e
and for executing the following statement python3.8 i.pyc every 5 min I wrote
5 * * * * #!/usr/bin/python3.8 i.pyc
Then, I made sure that no error using sudo less /var/spool/mail/root and saw the crone job in the list with sudo grep CRON /var/log/cron
But unfortunately, the corn is not running, I don't see the output file but if I write python3.8 i.pyc without the crone, the statement works well.
What I missed ?
Related
On Raspberry Pi 3B+ running on Raspberry Pi OS 64.
Trying to make a Python script executed every minute but don't work.
To edit crontab, I use :
sudo crontab -e
And put this line in file :
*/1 * * * * sudo /bin/python3 /home/pi/Documents/script_test01.py`
I also tried : */1 * * * * sudo python3 /home/pi/Documents/script_test01.py
Here my script, simply publish in a MQTT broker (script works by direct call in shell: python3 script_test01.py):
#!/usr/bin/env python3
import time
import paho.mqtt.client as mqtt
client_mqtt = mqtt.Client("Script-crontab")
client_mqtt.connect("localhost")
client_mqtt.publish("RandomTempValuesSimulator", "Hello !")
client_mqtt.disconnect()
exit()
I did a stop and start cron service with :
sudo systemctl stop cron.service
sudo systemctl start cron.service
Nothing more happened.
Make sure your script is executable. The interpreter is usually at /usr/bin/python3 (not /bin/python3) but you don't need that since you have the #! on top of your script.
Why are you editing root's crontab? Why not just your own? Check your /var/log/syslog files for any errors in the execution.
Solved.
When i did crontab -l, i've got no crontab for pi user.
Problem solved without "sudo" with command crontab -e and edit the blank crontab file openned and put this command : */1 * * * * /bin/python3 /home/pi/Documents/script_test01.py
I've been trying to debug this for a while now and I feel like I've tried everything.
Code is slightly modified with *** for company reasons.
The following executes as expected when run from a session as my local user.
/var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
Where run.sh is just a wrapper for running Python in a virtualenv:
#!/usr/bin/env bash
wd=$(dirname $0)
source ${wd}/virtualenv/bin/activate
python ${wd}/$1
I have placed a print statement inside of the Python main to show that it's being executed.
if __name__ == "__main__":
print("I got in here...")
When running the command as my local user, the log will contain this printed statement. However, when run in cron as:
*/30 * * * * /var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
I do not get any printed statement, nor do I receive any error output from the 2>&1.
My permissions are 755 on both the .sh and .py scripts.
Everything works as expected except when run via cron.
Am I missing something? Does cron not use .bashrc for the crontab user?
First make sure your local cronjob is running by putting the following in the crontab file and check to see if it get written out at /tmp/env.output after a minute or two
* * * * * env > /tmp/env.output
Second make sure the user running the crontab has permission to write to the /var/www/****/***/test.log file
Third try changing your script to
wd=$(dirname $0)
cd $wd
source activate
python ${wd}/$1
Edited: Anders was able to figure out the answer by himself by adding PYTHONPATH to the cron environment: export PYTHONPATH="${PYTHONPATH}:${wd}"
I have a crontab running on Linux 3.10.0-862.9.1.el7.x86_64 x86_64. In it, I have
a python script that prints out certain files to delete, and then I pipe it to xargs rm.
for example, i would run the script python delete_these.py and it would print out the following:
/directory/to/delete1.txt
/directory/to/delete2.txt
/directory/to/delete3.txt
/directory/to/delete4.txt
and these would be deleted through | xargs rm run on the same command prompt. so
python delete_these.py | xargs rm
The script runs fine when run manually, however when it's run on cron, it returns an error in stdout retrieved from the mail when the job is run:
rm: cannot remove '/directory/to/delete1.txt': No such file or directory
here's what i input in crontab:
* * * * * ssh [confidential#stuff.com] python /location/where/my/python/script/is | xargs rm
i've done chmod +x /path/to/python/script and putting the full command (minus the stars, but including the ssh) works fine on the command line, but running it on cron returns the 'no such file or directory' error.
I am sure that these files exist, and they are not symblinks.
i've retried it with attaching the associated absolute directory for python (usr/bin/) but it still doesn't work.
i have no idea why the cron doesn't work. the current PATH on the crontab set by another user is a directory to usr/bin/stuff that is specific to that box.
I thought it would be an issue with the PATH being different, but I don't think this is the case as I specify absolute directories within the command.
any ideas? i'm totally stumped on this
you're running the python command on one computer and the rm on another computer
you should probably quote the pipe (|) so it doesn't separate things
at a guess the crontab line should be:
* * * * * ssh [confidential#stuff.com] "python /location/where/my/python/script/is | xargs rm"
I checked a lot posts with the same title, but I can't get my python running via cron.
I have several cron scripts already, which execute well, but not python.
Crontab runs as root.
I added following lines to crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
I have this line for the execution in crontab:
* * * * * cd /var/www/usi/; /usr/local/bin/python3.6 /var/www/usi/usi.py
I tried a lot of variations:
added sudo in front of it to run as a different user
added bash to the line
executed the user profile before etc etc.
No results.
No errors in system log.
Any ideas?
Using Debian8
I found the error while putting the cron task into a separate shell script. I executed the script via cron and got an error. Simple typo. Did now saw any error before in syslog. :-(
It executes well with this in crontab now:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * cd /var/www/usi/ && /usr/local/bin/python3.6 /var/www/usi/usi.py
I am trying to setup a crontab to run 6 python data scrapers. I am tired of having to restart them manually when one of them fails. When running the following:
> ps -ef | grep python
ubuntu 31537 1 0 13:09 ? 00:00:03 python /home/ubuntu/scrapers/datascraper1.py
etc... I get a list of the datascrapers 1-6 all in the same folder.
I edited my crontab like this:
sudo crontab -e
# m h dom mon dow command
* * * * * pgrep -f /home/ubuntu/scrapers/datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out
Then I hit control+X to exit and hit yes to save as /tmp/crontab.M6sSxL/crontab .
However it does not work in restarting or even starting datascraper1.py whether I kill the process manually or if the process fails on its own. Next, I tried reloading cron but it still didn't work:
sudo cron reload
Finally I tried removing nohup from the cron statement and that also did not work.
How can I check if a cron.allow or cron.deny file exists?
Also, do I need to add a username before pgrep? I am also not sure what the "> test.out" is doing at the end of the cron statement.
After running
grep CRON /var/log/syslog
to check to see if cron ran at all, I get this output:
ubuntu#ip-172-31-29-12:~$ grep CRON /var/log/syslog
Jan 5 07:01:01 ip-172-31-29-12 CRON[31101]: (root) CMD (pgrep -f datascraper1.py ||
python /home/ubuntu/scrapers/datascraper1.py > test.out)
Jan 5 07:01:01 ip-172-31-29-12 CRON[31100]: (CRON) info (No MTA installed, discarding output)
Jan 5 07:17:01 ip-172-31-29-12 CRON[31115]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jan 5 08:01:01 ip-172-31-29-12 CRON[31140]: (root) CMD (pgrep -f datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out)
Since there is evidence of Cron executing the command, there must be something wrong with this command, (note: I added the path to python):
pgrep -f datascraper1.py || /usr/bin/python /home/ubuntu/scrapers/datascraper1.py > test.out
Which is supposed to check to see if datascaper1.py is running, if not then restart it.
Since Cron is literally executing this statement:
(root) CMD (pgrep -f datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out)
aka
root pgrep -f datascraper1.py
Running the above root command gives me:
The program 'root' is currently not installed. You can install it by typing:
sudo apt-get install root-system-bin
Is there a problem with Cron running commands from root?
Thanks for your help.
First of all, you need to see if cron is working at all.
Add this to your cron file (and ideally delete the python statement for now, to have a clear state)
* * * * * echo `date` >>/home/your_username/hello_cron
This will output the date in the file "hello_cron" every minute. Try this, and if this works, ie you see output every minute, write here and we can troubleshoot further.
You can also look in your system logs to see if cron has ran your command, like so:
grep CRON /var/log/syslog
Btw the >test.out part would redirect the output of the python program to the file test.out. I am not sure why you need the nohup part - this would let the python programs run even if you are logged out - is this what you want?
EDIT: After troubleshooting cron:
The message about no MTA installed means that cron is trying to send you an e-mail with the output of the job but cannot because you dont have an email programm installed:
Maybe this will fix it:
sudo apt-get install postfix
The line invoking the python program in cron is producing some output (an error) so it's in your best interests to see what happens. Look at this tutorial to see how to set your email address: http://www.cyberciti.biz/faq/linux-unix-crontab-change-mailto-settings/
Just in case the tutorial becomes unavailable:
MAILTO:youremail#example.com
You need to add python home to your path at the start of the job, however you have python set up. When you're running it yourself and you type python, it checks where you are, then one level down, then your $PATH. So, python home (where the python binary is) needs to be globally exported for the user that owns the cron (so, put it in a rc script in /etc/rc.d/) or, you need to append the python home to path at the start of the cron job. So,
export PATH=$PATH:<path to python>
Or, write the cron entry as
/usr/bin/python /home/ubuntu/etc/etc
to call it directly. It might not be /usr/bin, run the command
'which python'
to find out.
The 'No MTA' message means you are getting STDERR, which would normally get mailed to the user, but can't because you have no Mail Transfer Agent set up, like mailx, or mutt, so no mail for the user can get delivered from cron, so it is discarded. If you'd like STDERR to go into the log also, at the end, instead of
"command" > test.out
write
"command" 2>&1 > test.out
to redirect STDERR into STDOUT, then redirect both to test.out.