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

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.

Related

CRON job outputting blank lines

I tried to create a CRON job that runs every minute. As a root user, I ran crontab -e, and in the cronjob, I put in my command * * * * * /usr/bin/php {redacted}/index.php > {redacted}/output.txt
the {redacted} file path has permissions added: chmod ogu+rwx -R so everyone should be able to access it. I even created a new user with no sudo or root privileges, and running /usr/bin/php {redacted}/index.php > {redacted}/output.txt obviously writes the output of {redacted}/index.php to {redacted}/output.txt. However, my cron job ends up overwriting and turning the .txt file to just blank, nothing. I have no idea what is going on since I already made sure there were no permission errors, and cron jobs don't seem to have a visible log or output?
I would love to have any ideas about this. I even tried to add the cron job * * * * * root /usr/bin/php {redacted}/index.php > {redacted}/output.txt, all to the same output.
One more thing... I have tried to set the {redacted} location to /var/www/html, /var/www/{a user with sudo perms}, and {/usr/local/bin} all with the same result.
Update:
I tried something as simple as * * * * * php -v > {redacted}/output.txt and * * * * * /usr/bin/php -v > {redacted}/output.txt still with the same result, so it seems like the error is not with requiring root permissions to run my php program

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.

How to reboot via cron on scheduled basis. Ubuntu 14.04

I have a very simple script that works from the command line.
#!/bin/bash
reboot
When I put a call to execute the script into root users crontab -e using the following format it does not run. It does run the first two commands, just that last one is giving me grief. I have no MTA installed as I do not need it.
*/10 * * * * service jwtpay restart
0 3 * * * bash /root/backup/mongo.backup.s3.sh kickass /root/backup >/dev/null 2>&1
0 */3 * * * bash /root/reboot.sh >/dev/null 2>&1
What am I missing?
Maybe the script is not executable... Since you use root's crontab why call the binary via a script and not the binary itself? Use the full path to the binary. It may vary on your system. Find out where it is with which reboot.
0 */3 * * * /sbin/reboot
Don't forget to restart the cron daemon, after changeing the crontab.

Can't make crontab work

I am new to Linux and Ubuntu and I seldom have to use it. I am trying to make this PHP script to run every minute using cron, but firstly I wanted to make some tests.
I created an empty file at /var/www/html/ called test. I ran on terminal:
sudo crontab -e
And added this line:
0 * * * * rm /var/www/html/test
Then saved it and exited. It said "Installing new Crontab"
Nothing happened. Then I created a file bfile.sh that contained:
#!/bin/sh
rm /var/www/html/test
and added the following to crontab:
0 * * * * bash /var/www/html/bfile.sh
Still nothing happened.
What do I have to do to see anything happening from crontab? By the way I checked and the service is running
0 * * * * basically says "run this at 0th minute of every hour."
If you need cron to run your command every minute do * * * * *.
0 * * * * runs once every 1 hour. If you want to run every minute it should be */1 * * * *
You can also check the /var/log/cron file for any errors

Resources