How do I execute cron service in linux? - linux

I am trying to run a Python code every minute in Linux but at loss to configure cron service. Following is what I added after running 'crontab -e' but nothing is happening.
* * * * * cd /home/kali && /usr/bin/python3.8 /home/kali/time.py
time.py contains simple code to show current time.
from datetime import datetime
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
Any suggestions?

Firstly you need to add it to crontab,try:
sudo nano /etc/crontab
and after that you can put your service:
* * * * * cd /home/kali && /usr/bin/python3.8 /home/kali/time.py
Check it again 'sudo crontab -e'

you should run your code with python3. i dont understand why you use cd command, so i omit that.
This is the way you can run your python code in cron:
* * * * * python3 /home/kali/time.py
Note: Check if you have permission to execute .py file.

Related

Cronjobs do not run

I'm trying to run a cronjob to start and stop a server under a non-sudo user. I've tried asking others and doing what I saw from looking on google before asking here, but I'm still stuck.
Here's what's in my crontab for the server user:
* * * * * /home/server/startup/stop.sh
* * * * * /home/server/startup/start.sh
Here is what is in my stop.sh script:
#! /bin/sh
screen -r server -X quit
Everything runs normally if I run it using sh, and I only encounter a problem when using cron.
From what I see there could be 2 possible problems:
If the lines you are running in crontab are (and only those):
home/server/startup/stop.sh
home/server/startup/start.sh
then you are missing the time part of the line. If you want to run your program only once on boot you can run:
#reboot home/server/startup/start.sh
You are not giving the full path to your program (possibly you are just missing a / in the begging). Try running
* * * * * /home/server/startup/start.sh
or
#reboot /home/server/startup/start.sh
If these don't work I recommend you try the following to troubleshoot the issue:
Run the command using sh in the cron:
* * * * * /bin/sh /home/server/startup/start.sh
Try redirecting the stdout and stderr of your command to a file and see if any errors occur

Pi Cron Job Fails to Run

Hoping someone can help with a crontab issue I am having on the pi.
I have a program, rf24_receiver.py, that resides in the /home/pi directory. When run from terminal, it requires sudo to run. So with this command on terminal,
sudo python3 rf24_receiver.py, the program executes perfectly.
However, I need to run it on a schedule, so I tried the following without success.
crontab -e
*/30 * * * * sudo python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
*/30 * * * * python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
sudo crontab -e
*/30 * * * * sudo python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
Then I tried this
*/30 * * * * python3 /home/pi/rf24_receiver.py >> /home/pi/rf24_receiver.py.log 2>&1
No success with any of the above. I hope this is the right place to post this.
Any help would be appreciated.
Thank you
Sorry to bother the forum with this question. I am not sure why I got this command to work under sudo crontab -e:
*/29 */1 * * * sudo python3 /home/pi/rf24_receiverR3.py >> /home/pi/rf24_receiverR3.py.log 2>&1
I did a reboot and it seemed to work after that.

How to make crontab run new version of my script

I have currently running python script on debian system. Now because of some reasons I changed this script and updated cron, but nothing has changed. Also, I tried to save this cron in different file and create new cron - line with job appears, but script doesn't work.
CRON[22310] (root) CMD ( /usr/bin/python /home/radmin/test/test.py)
from /etc/crontab for new script:
*/1 * * * * root /usr/bin/python /home/radmin/test/test.py
for old script:
*/1 * * * * root python /home/radmin/base.py
Script runs correctly without cron.
Tried restarting and reloading cron.
It looks like cron dosen't recognize the root command you add.
try instead opening cron using sudo crontab -e and then add your code:
*/1 * * * * /usr/bin/python /home/radmin/test/test.py
by opening it up with sudo it will add it to the root user cron jobs.
Problem was in python code. Crontab is okay. I'm using python lib "requests" and there is a method to get post request content - ".text", so this method doesn't want to work in cron (empty error logs while running) (still don't know why). So changing .text to .content solved this issue.

Why is my crontab -e not running my python script?

I want to run hello.py file which contains print("Hello World") using crontab.
For that, my hello.py has this code:
#! /usr/bin/python3
print('Hello, world!')
And, in the same folder, I have used crontab -e command to open crontab and in order to execute this file every minute, I have written:
1 * * * * ./hello.py
I have also set permissions for the file to be executable using chmod a+x hello.py.
When I run
/usr/bin/python3 hello.py
It runs perfectly. Also, when I use only ./hello.py, the file runs.
Why is it still not executed using crontab?
Nailed it!
Instead of using 1 * * * * ./hello.py in crontab to set the cron running per minute , I rewrote the statement to 1 * * * * /usr/bin/python3 hello.py .
This solved the problem!

I can't get crontab to run, sudo python3 to run script an issue?

I have been researching this topic for the past two hours and can't find similar info. I am putting the last touch on a LED sign and I want it to run the script every x amount of minutes from raspberry to update the info going to the sign, lets just say every 10 minutes. I have tried everything with crontab -e and sudo crontab. my question is I have to run the file (mysign.py) from the directory in cd my_python and then from there I have to use the command sudo python3 mysign.py, it will not run with sudo python. I am wondering if this has anything to do with it?
here's some of what I have tried, along with the #reboot as well with nothing.
/10 * * * * /usr/bin/python mysign.py
/10 * * * * /usr/bin/python3 mysign.py
/10 * * * * /usr/bin/python /home/pi/my_python/mysign.py
/10 * * * * /home/pi/my_python/mysign.py
First of all, to execute on every 10th minute you need to use */10 ... not /10 ....
Second, entries from root's crontab execute as root, hence their home is not /home/pi - you actually need to specify the whole path for both the interpreter and the script:
*/10 * * * * /usr/bin/python3 /home/pi/my_python/mysign.py
Make sure you set it in the root's crontab (sudo crontab -e).
This, of course, assumes the location of your python3 interpreter and the script itself, if those paths are not correct - correct them before adding to crontab.

Resources