Created cron job to run every 2 mint - cron

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.

Related

Not able to run a python script after every 5mins using cron

I edited the crontab with -e option. Then I went to /etc/cron.d directory to create a file which would run the process. Again I edit the /etc/crontab file. But I couldn't made it run. I refered to this article from stackoverflow and did exactly same but I don't know why cron is not working for me ?
here is how my crontab looks like -
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * anikde /home/anikde/Documents/pythonProjects/python_scripts/*
* * * * * anikde python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
I have set the 1st job to run after every 5 minutes and 2nd job after every one minute. But none of jobs are running automatically. While they actually run when I command the job to run first script as bash script and the second file as python script.
I assume you are trying to automatically run below script every 5 minutes:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
First, determine the location of your Python executable, using which python command. In below examples I assume the returned path to be /usr/bin/python.
If editing your own crontab (crontab -e) try this command:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If no user is specified, the job is run as the user that owns the crontab file, using his environment. But you can also try adding the username
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
If editing root crontab (sudo crontab -e)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py

How the cron timing is working?

Suppose, current time is 11:42 and i have setup one cron file to run at every 5 minutes.
Then this file will run at which time 11:47 or 11:45?
So basically i am trying to understand that how the cron timing is work?
Edit : it was ran at 11:45, but i don't know the reason behind it
Cron Configuration :
*/5 * * * * wget -O /dev/null http://XXX/index.php?r=controller/action
As you know, cron will run jobs at a specific time.
A cron job will not use the time it was started, only the configuration matters. This means a cron job set to every 5 minutes (like your */5 * * * *) will only ever run at times ending with 0 or 5 (eg: 12:00, 12:05, 12:10), regardless of the time you run it. This makes sense because we want to schedule a job for a specific time.
If you really need a job to run every 5 minutes, with an offset (eg: 11:42, 11:47, 11:52) you will have to give a list in the configuration.
instead of (*/5 * * * *) you would need to use:
(2,7,12,...,57 * * * *), filling ... with all the other numbers.

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.

Scheduling shell script in crontab

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

Cron runs after other cron

I want to set a cron run after an other cron. For example: Cron A finishs at 01:00 PM, cron B will start at 01:01 PM. The problem is I don't know when cron A finishs.
I checked the crontab syntax. It doesn't provide any param for that purpose.
My actual situation is:
# This cron must run first.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
# two these crons runs at the same time.
0 13 * * * /usr/local/bin/php -f /path/update_user.php
0 13 * * * /usr/local/bin/php -f /path/update_image.php
# This cron runs right after two above cron completes.
? ? * * * /usr/local/bin/php -f /path/select_and_print_to_log_file.php
You can use the batch command inside the first cron to have the second thing being scheduled to run.
Your first job could produce a timestamp when finished.
Then you estimate - for example - that job A needs about 60 to 90 minutes. After 60 minutes, you start job B. Job b looks for the timestamp. If it is present, job B starts, else it waits for a minute and looks again.
After finishing, job B deletes the timestamp, or renames it, maybe from 'todo' to 'done'. You could insert the current date inside the file, to check, whether your estimation is still acceptable, or should be adjusted.
What I do in such cases (commonly a backup scenario where I don't want to thrash the disk by having concurrent backups) is to write a script that cron calls, and in the script have the actual tasks run serially.
Something like:
#!/bin/bash
/usr/local/bin/php -f /path/update_user.php
/usr/local/bin/someOtherTaskToRunSecond
YMMV.

Resources