Scheduling shell script in crontab - linux

I have a shell script that I can run with root user this way:
root#vivid-15:~# ./backup.sh
It's on /root/backup.sh. Now, how do I schedule it on crontab to execute every day at 01:00 AM? I done this:
0 1 * * * root
But now I don't know how to proceed the command to do that.

Have you tried this? Also, a "1" in the hour field means 1am, not 1pm.
0 1 * * * root /root/backup.sh
Edit: changed the 13 (1pm) back to 1 (1am).

Crontab format:
MIN HOUR DAY MON WEEKDAY CMD
I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e
I think you're looking for something more like this:
0 1 * * * /root/backup.sh

Related

Created cron job to run every 2 mint

I have configured cron job but it's not working.
I wanted to run the myfile.sh script for every 2 mint and below are my configuration in crontab.
# m h dom mon dow comman
2 * * * * /home/ubuntu/myfile.sh
myfile.sh is executable and contains below lines of code
#!/bin/bash
mysqldump -u[user] -p[password] --single-transaction --routines --triggers --all-databases > /home/ubuntu/backup_db10.sql
Is there anywhere we need to add configure anything?
You're running the script at two minutes past every hour. As in 1:02, 2:02 and so on.
You can change it to something like
*/2 * * * * /home/ubuntu/myfile.sh
to run it every two minutes.
A bit more info can be found here.

Crontab command not working

I know this question has been asked many, many times and I've done a lot of research but still I'm not able to run this extremely simple cron:
$ crontab -l
* * * * * /bin/date
This should, ideally, print the date every minute.
There is no cron.allow or cron.deny file, and the cron daemon is working:
ps -e | grep cron
1119 ? 00:00:00 cron
17646 ? 00:00:00 cron
Any idea what might be wrong?
Cron processes run in a separate subprocess of their own, so the output of a cron job will not be visible to you in your shell.
Instead, you will have to capture the output of your cron commands and save them. So, set your cronjob like:
* * * * * /bin/date >> /home/user/date.log
And now, if you will tail this log file, you will start seeing the result.

Why my scheduled job is not working?

I define a job with crontab like this
0 2 * * * dbadmin . /home/dbadmin/back.sh
it is not root I want to run this .sh file with dbadmin user.
but when I checked it is not working.
in the log it gives this:
Feb 22 21:16:01 localhost crond[14634]: (*system*) BAD FILE MODE (/etc/crontab)
Feb 22 21:16:01 localhost crond[14634]: (dbadmin) RELOAD (cron/dbadmin)
Feb 22 21:16:01 localhost crond[28451]: (dbadmin) CMD (dbadmin . /home/dbadmin/back.sh)
How can I fix this? thanks in advance
Make a crontab entry as dbadmin without the username in it:
0 2 * * * /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Each day the logfile /home/dbadmin/cron.out should be replaced by a new one.
When you are confident about the cron+movefolder, replace the outputfile with /dev/null.
When above fails, check calling the script as dbadmin:
sh /home/dbadmin/movefolder.sh
When this one works and cron fails, it might be the environment. Try saomething like
0 2 * * * . /home/dbadmin/.profile; /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Using the crontab program, you normally have access only to the 5 scheduling fields (minute, hour, day of month, month and day of week). However, with Vixie cron (usually on Linux) by editing the system crontab file (/etc/crontab, as well as files in /etc/cron.d) you can use the 6th field for the username. For example, see How to specify in crontab by what user to run script?
If you use crontab to enter this line
0 2 * * * dbadmin . /home/dbadmin/back.sh
^^^^^^^
the "dbadmin" username is treated as the command to execute. You can (as noted in crontab's manual page) use that line in /etc/crontab. I pointed out that this is Vixie (also known as ISC) crontab. Legacy systems such as Solaris have a less capable crontab which would not allow specifying the user to run under.
According to cron's manual page, it will send output via email. Perhaps there was no email because the command "dbadmin" failed.
You need to use sh command for executing sh file.like following
0 2 * * * dbadmin sh /home/dbadmin/movefolder.sh
Hope it works.
Thank you.

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.

Using Cron to Reboot

I'm using a Raspberry Pi for a status display, but for whatever reason it gets incredabbly sluggish after a day or so of running so I wanted to reboot it every day so I setup a cron job to do that every morning at 8:50. But, it doesn't seem to be working. Is there anything special about using cron to do a reboot?
This is my crontab for the root user:
# m h dom mon dow command
50 8 * * * shutdown now -r >> /var/log/cron.log
0,30 * * * * date >> /var/log/cron.log
The second line works just fine, but I can't seem to get the restart command to work. It doesn't even output anything to the log.
Try using the fully specified path to shutdown. date may be in the PATH in roots cron environment, /sbin may not be looked up.
You need to edit the root user crontab
sudo crontab -e
then..
50 8 * * * reboot
Save and exit.

Resources