CronTab every 5 minutes reboot doesn't work - linux

I've put the command
*/5 * * * * root /sbin/shutdown -r now
Inside "sudo crontab -e" on a Raspberry Pi, which should reboot the raspberry pi every 5 minutes. But instead nothing happens.
I've looked in "/var/log/syslog" but all it says is
Sep 24 08:55:01 raspberrypi CRON[638]: (root) CMD (root /sbin/shutdown -r now)
Sep 24 08:55:01 raspberrypi CRON[634]: (CRON) info (No MTA installed, discarding output)
Hoping someone can help me!
Thanks, Jake.

It's not a valid command, remove the "root".
Since you edited the root crontab (sudo crontab -e), the command is already executed as a root.

Related

crontab not working despite trying different fixes from Stackoverflow

I'm trying to run some cron jobs as root but it seems that my crontab it's not working. After reading a lot of threads here in SO I've changed these things:
I've removed the sudos inside the crontab.
I've added the full path of all the commands inside the crontab.
I've restarted the cron job each time I've changed it.
But it's not working yet.
This is my current crontab (I call it from my sudo user with "sudo crontab -e" to run it as root):
0 4 * * * /usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
5 4 * * * /usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
30 4 * * * /opt/letsencrypt/certbot-auto renew > /var/log/log3.log
35 4 * * * /etc/init.d/nginx reload > /var/log/log4.log
40 4 * * * /bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
What I'm trying to do with these 5 commands is:
At 04:00. Remove all backups that are more than 7 days old.
At 04:04. Backup all my mongodb databases.
At 04:30. Try to renew my SSL certificate.
At 04:35. Reload Nginx service.
At 04:40. Update my mongodb.pem file with the new certificate.
It's important to say that each of these commands running from the shell one by one with sudo are working properly.
But from the cron the results are:
I don't know if this command is working. The log file (log1.log) is empty.
It's not working. No backup have been created. The log file (log2.log) has not been created.
It seems that it's working. The log file (log3.log) shows the standard output when you run this command correctly.
It seems that it's working. The log file (log4.log) shows the standard output when you run this command correctly.
It seems that it's not working. The mongo.pem file has the correct updated date but the file is empty. The files fullchain1.pem and privkey1.pem have the correct content, so it seems a problem with "/usr/bin/tee /etc/ssl/mongo.pem". The log file (log5.log) has not been created.
Finally the cron.log shows this:
Nov 11 04:00:01 myservername CRON[31286]: (root) CMD (/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log)
Nov 11 04:05:01 myservername CRON[31297]: (root) CMD (/usr/bin/mongodump --out /var/backups/mongobackups/`date +")
Nov 11 04:05:01 myservername CRON[31296]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:07:01 myservername CRON[31306]: (root) CMD ( test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Nov 11 04:17:01 myservername CRON[31325]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 11 04:30:01 myservername CRON[31353]: (root) CMD (/opt/letsencrypt/certbot-auto renew > /var/log/log3.log)
Nov 11 04:30:04 myservername CRON[31352]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:35:01 myservername CRON[31393]: (root) CMD (/etc/init.d/nginx reload > /var/log/log4.log)
Nov 11 04:40:01 myservername CRON[31420]: (root) CMD (/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log)
Nov 11 04:40:01 myservername CRON[31419]: (CRON) info (No MTA installed, discarding output)
If I'm not wrong the error "No MTA installed, discarding output" is not a problem because it's related to sending the output by email, am I right?
So anyone could tell me what is wrong with my crontab?
Thank you very much!
I don't know what is the problem with my crontab but this is working if I put all these commands inside a script and call this script inside the crontab.
The script (myscript.sh):
#!/bin/bash
/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
/usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
/opt/letsencrypt/certbot-auto renew > /var/log/log3.log
/etc/init.d/nginx reload > /var/log/log4.log
/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
The crontab:
00 03 * * * <pathtomyscript>/myscript.sh
This is working now.

Crontab does not run on CentOS 7

I'm using CentOS 7, and installed "cronie"
yum install cronie
I have a shell to backup my home folder, shell's content (of course, backup.sh is 775)
#!/bin/bash
#START
TIME=`date +%Y-%m-%d_%Hh%M`
FILENAME=backupHome_123.30.150.29_$TIME.tar.gz
SRCDIR=/home
DESDIR=/backup
tar -cpvzf $DESDIR/$FILENAME $SRCDIR
#END
And add to crontab -e
00 2 * * * /bin/bash /backup/backup.sh
But crontab does nothing. When I check log at /var/log/cron only, something like that and no more error or processing log
May 1 00:26:08 app crontab[12041]: (root) END EDIT (root)
May 1 00:33:21 app crontab[12086]: (root) BEGIN EDIT (root)
May 1 00:34:25 app crontab[12086]: (root) END EDIT (root)
Anyone can give me some advises to make crontab work?
Thank you.
I found that the most useful information was actually given by
systemctl status crond
Which revealed that it failed to load correctly due to an "Unauthorized SELinux context" error.
This can happen if cron daemon isn't running.
Check it with:
pgrep cron
If command returns nothing, run:
systemctl restart crond
This should help.
You want to make sure cron is started & that it is started if your server reboots so you need two commands on CentOS 7 to make sure of this:
systemctl enable crond && systemctl restart crond
If you are used to using sudo, you can add sudo in front of above command. like this:
sudo systemctl enable crond && systemctl restart crond
YOU WILL get confirmation from system, something like:
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
Then lastly check cron is running
pgrep cron
I know that I'm late to answer you, but maybe someone will have this kind of problem. It is possible that CRON can't run the script because the path is not correct.
Your path is
00 2 * * * /bin/bash /backup/backup.sh
I guess that path should be:
00 2 * * * sh /bin/bash/backup/backup.sh
Only difference is space after bash directory and sh command at the start of CRON job.
You have to add username before your command like this for example :
1 * * * * root or username /usr/bin/php /var/www/html/yourwebsite/yourscript.php

Cron not running for user

I want a user (tcff) to run two python scripts at 2am every morning.
I have correctly installed the following crontab for this user:
tcff#mymachine> crontab -l
0 2 * * * python /home/tcff/path/to/myscript1.py
0 2 * * * python /home/tcff/path/to/myscript2.py
The permissions for each script are:
-rwxr-xr-x 1 tcff tcff 5522 Sep 25 12:41 myscript1.py
-rwxr-xr-x 1 tcff tcff 5522 Sep 25 12:41 myscript2.py
When I call each script directly they work fine:
tcff#mymachine> python /home/tcff/path/to/myscript1.py
[Output as expected]
However they are not being run by cron at 2am each morning.
I can't work this out. I am sure I have the permissions correct etc?
Yes, indeed, the reason the scripts were not running is because I did not use the full path to the Python binary:
0 2 * * * /usr/bin/python /home/tcff/path/to/myscript2.py
This is needed because although the shell (bash) has /usr/bin on the PATH the process running cron does not.
First of all give full permission to script file.
chmod 777 script_name
Also trace the logs of crontab and see what is happening with cronjob here you can see all logs of crontab. May be there is any exception or error.
root#localhost:[~]: tail -f /var/log/cron

Shell script runs manually but not executed through cron job

I am using a shell script to delete log files in my Linux machine. And I have used crontab to execute this job periodically. I can execute this script manually and see it is working fine. But the script is not triggered through cron job.
Shell script:
[root#testVM-dev log]# vim /path_to_dir/log_cleanup.sh
#!/bin/bash
now=$(date)
echo "Cron Job to delete log files start...# $now" | tee -a /path_to_dir/log/cronjob_log.log
/usr/bin/find /path_to_dir/log/localhost_access_log.* -type f -mtime +5 -print -delete | tee -a /path_to_dir/log/cronjob_log.log
echo "Delete files completed!" | tee -a /path_to_dir/log/cronjob_log.log
crontab entry: (Runs every minute)
[root#testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
Also I have checked in cron logs that the job is executed every minute. But I couldn't see any logs in the targeted log file "cronjob_log.log"
cron logs:
vim /var/log/cron
Jul 16 03:56:01 rstestVM-dev crond[19131]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:57:01 rstestVM-dev crond[19150]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:58:01 rstestVM-dev crond[19168]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:59:01 rstestVM-dev crond[19188]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Can someone help me on what is going wrong here?
Besides manually specifying value of PATH, try specifying the path to tee and date.
/usr/bin/date, /usr/bin/tee
I have found the solution. That I have removed the user from crontab entry & it worked.
Original crontab entry:
[root#testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
After modification:
[root#testVM-dev log]# crontab -l
*/1 * * * * /path_to_dir/log_cleanup.sh
Now I got confused why the user entry in cron job didn't work? Can someone explain?

crontab need user to login once before it runs the script

I'm trying to schedule a game server restart and vps restart at a time. I made 3 cron entries (screen terminate, vps reboot, game server start)
crontab entry for root user
30 6 * * * /sbin/reboot
crontab entry for user1
29 6 * * * sleep 48; screen -S cs1 -X quit
31 6 * * * cd /home/user1/steamcmd/hlds; ./cs1.sh
cs1.sh
screen -dmS cs1 ./hlds_run -game cstrike -pingboost 2 +map de-dust +maxplayers 31 +port 27018
The problem is the user1's second crontab entry is not working unless if I log in (via putty) after the reboot and before 6:31 AM. Can anyone experienced in this explain how to fix that?
I run a craftbukkit server that reboots every day at 0230. I found I couldn't call the startup script from crontab, but I could just run the screen command.
Here is my crontab entry for the server restart:
#reboot cd /home/mcadmin/craftbukkit && screen -dmS minecraft java -Xincgc -Xmx2048M -jar craftbukkit.jar

Resources