Pi Cron Job Fails to Run - cron

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.

Related

Running node script with crontab

I'm trying to run a node script with crontab. I've tried first doing things like
* * * * * echo test > test.txt
to be sure crontab works (I'm trying to make the command work and then I'll change the crontab to something different so it doesn't run every minute).
The crontab above works. The thing is, when I try to use node, it doesn't run with the crontab. Running which node I get /usr/bin/node Here are the things I've tried.
Thanks!
* * * * * cd /path/to/script && node script.js
* * * * * cd /path/to/project && npm start (runs npx tsc && node build/script.js)
* * * * * cd /path/to/script && node script.js > test.txt (file is generated empty, even though, script has console.log)
* * * * * node /path/to/script/script.js
* * * * * echo test > test.txt && node /path/to/script/script.js (file gets generated)
Also I've tried all of the above replacing node by /usr/bin/node.
If I run any of these commands manually, it executes the program.
After going over a long time and testing a lot of stuff, I realized the issue was doing sudo crontab -e to set the crontab. I fixed it after running instead
sudo crontab -u username -e

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

How do I execute cron service in 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.

Cron jobs not running in VestaCP (CentOs)

I have written some PHP scripts that I am trying to run with cron jobs in VestaCP, but they don't seem to be running. I have tried to search for other threads on here and on the VestaCP forum that could help me identify the error, but have not found a solution.
Server system:
CentOs 7.4
Vesta 0.9.8-22
I have tested the PHP scripts by going to the links directly. They all work well. But the cron jobs are not running and I can't figure out why.
I have not been able to locate any error messages or logs generated by these cron jobs (even when I remove the "> /dev/null 2>&1"). But I might have been looking in the wrong places.
All of the cron jobs have been added through the VestaCP cron interface.
I have disabled exim, dovecot, clamd, and spamassassin. And I have turned off notifications in the cron panel. Not sure if that is related.
Copied from /var/spool/cron/admin [edited domain name]:
15 02 * * * sudo /usr/local/vesta/bin/v-update-sys-queue disk
10 00 * * * sudo /usr/local/vesta/bin/v-update-sys-queue traffic
30 03 * * * sudo /usr/local/vesta/bin/v-update-sys-queue webstats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue backup
10 05 * * * sudo /usr/local/vesta/bin/v-backup-users
20 00 * * * sudo /usr/local/vesta/bin/v-update-user-stats
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-rrd
15 6 * * * sudo /usr/local/vesta/bin/v-update-sys-vesta-all
01 4 * * * sudo /usr/local/vesta/bin/v-update-letsencrypt-ssl
*/5 * * * * wget -q -O - "https://testing.example.com/cli/new-projects" > /dev/null 2>&1
*/5 * * * * wget -q -O - "https://example.com/cli/new-projects" > /dev/null 2>&1
30 10 * * * wget -q -O - "https://example.com/cli/project-expiration" > /dev/null 2>&1
*/5 * * * * sudo /usr/local/vesta/bin/v-update-sys-queue letsencrypt
0 10 * * * wget -q -O - "https://testing.example.com/cli/project-expiration" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://testing.example.com/cli/email-sender" > /dev/null 2>&1
*/2 * * * * wget -q -O - "https://example.com/cli/email-sender" > /dev/null 2>&1
I had the same problem in Ubuntu.
The problem was that the cron jobs created from VestaCP control panel is created for the user and although there is sudo at the beginning of the command, they are not run.
They seem to be called since I can see them on the /tmp/log/syslog file. It does not show any error though. But for some reason, the commands are not executed.
Here are few simple commands to check cron status.
Check if Cron service is running:
pgrep cron
if a number is returned the service is running else not
Check Cron status:
systemctl status cron
Check the current cron file:
crontab -l
Edit cronjob file:
crontab -e
One solution is to create the cron jobs for the root user from the terminal. I have not tried for other users. The cron jobs created for root user will run without a 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