Why my crontab request dont work to call a python script - python

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

Related

CronTab not run

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 ?

schedule a cron job to ping a server and run python script

I am trying to automate a bash script in Ubuntu. The script pings a server and then runs a python script if the packet is not received. The python script sends me a a notification when the ping is not returned. The script works when I run it manually, but it is not working when I schedule a cron job.
The bash script is named ping.sh.
#!/bin/bash
pingString=$(ping -c 1 google.com) # google is just and example, for my script I am using a server that intentionally does not return the packet.
msgRecieved="1 received, 0% packet loss"
msgLost="0 received, 100% packet loss"
if `echo ${pingString} | grep "${msgLost}" 1>/dev/null 2>&1`
then
python3 ping.py
fi
This is how I setup the cron job:
crontab -u username -e
* * * * * /bin/sh /home/username/Documents/ping.sh
I am confused because I set other dummy cron job for testing and it works fine. Example below:
* * * * * /bin/sh /home/username/Documents/test.sh
test.sh
#! /bin/bash
touch /home/username/Documents/ping_server/text.txt
The text.txt file is successfully created every minute.
Thanks for the suggestions. My problem was solved by
adding full path of the python script "ping.py" in the bash script
adding environment variables to crontab
I didn't know environment variables set in .bashrc are not loaded when running cron.
In Ubuntu it is possible to declare env variables before the jobs scheduled just like you would in bash.rc:
crontab -u username -e
ENV_VAR1=variable1
* * * * * /bin/sh /home/username/Documents/ping.sh

cronjob error in OSX: "no path for address"

I tried running a Python script using cronjob but I get the following error:
cron[44405]: no path for address 0x10ff7a000
in grep cron /var/log/system.log
When I ran the script without using cronjob it worked:
/usr/bin/python  /Users/anuj/Desktop/message.py
I tried adding the cron job using $sudo crontab. This is the CRON script:
*/1 11-17 * * 1-7 /usr/bin/python  /Users/anuj/Desktop/message.py
Both paths are correct for root mode and user mode as I am running cron with sudo.
try to create file like message.sh
inside it run your .py file
#!/bin/sh
python path/to/python_script.py
and make this file executable with chmod a+x message.sh
*/1 11-17 * * 1-7 path/to/message.sh 2>&1

Crontab does not run my python script usi.py

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

Crontab job won't trigger or save

Trying to run my first crontab job (a python script) on Mac OS X El Capitan.
In the terminal I run: crontab -e
which opens a crontab.xyz123 file in Sublime Text (as I set it up). I put in the following cron syntax:
30 * * * * /Users/user.name/anaconda/bin/python /Users/user.name/Desktop/python_script.py
I just want it to run every half hour. The python script opens a youtube video and I've included it below:
#!/Users/user.name/anaconda/bin/python
print("this is a script")
When I run crontab -l I get the following error:
crontab: no crontab for user.name - using an empty one
crontab: no changes made to crontab
How can I get this to run properly? Is something wrong with the cron job? With the script?
Other note: I've changed the script permissions to be executable and I'm using only full paths in the crontab itself. What else could be wrong? I've run the script by itself in command line and it works fine.

Categories