remove single cronjob from crontab - cron

I have a script that needs to be triggered every day at random times. The times the script needs to run are generated at midnight and stored in a mysql database. Occasionally on some days no times are generated and the script doesn't need to be triggered.
the entries can look like this:
+----------------+----------------------------+
| Date | Times |
|----------------+----------------------------+
| 2020-06-13 | 07:13, 14:48 |
| 2020-06-14 | |
| 2020-06-15 | 09:00, 09:26, 17:01, 17:42 |
+----------------+----------------------------+
I'm using php to grab the times and split them up in crontab format since I figured it might be the easiest way to run the script. I can generate something like this:
00 09 * * * /usr/bin/php /var/www/apps/control/trigger.php
26 09 * * * /usr/bin/php /var/www/apps/control/trigger.php
01 17 * * * /usr/bin/php /var/www/apps/control/trigger.php
42 17 * * * /usr/bin/php /var/www/apps/control/trigger.php
this would work, if I need to run them daily at these times. is there a way to remove the like when the script ran successfully? I cannot really use crontab -r to clear the jobs because there are other crontasks planned too.
The server i'm working on is a debian server.
Thank you very much in advance.

Related

How to set crontab every 1 hour 1 minute

I want to schedule a command every 1 hour and 1 minute. For example, if the first command executes at 01:01 pm, the next command will execute at 01:02PM; the time between the command executions is 1 hour and 1 minute.
I tried using
*/1 */1 * * *
but it runs every minute. Can anyone help me?
There's no way in crontab to schedule a job to run every 61 minutes (which, BTW, is an odd thing to want to do), but you can do it indirectly.
You can schedule a job to run every minute:
* * * * * wrapper_script
where wrapper_script invokes the desired command only if the current minute is a multiple of 61, something like this:
#!/bin/bash
second=$(date +%s)
minute=$((second / 60))
remainder=$((minute % 61))
if [[ $remainder == 0 ]] ; then
your_command
fi
This sets $minute to the number of minutes since the Unix epoch, 1970-01-01 00:00:00 UTC. You can adjust when the command runs by using a value other than 0 in the comparison.
That's assuming you want it to run every 61 minutes (which is what you asked). But if you want to repeat in a daily cycle, so it runs at 00:00, 01:01, ..., 23:23, and then again at 00:00 the next day, you can do it directly in crontab:
0 0 * * * your_command
1 1 * * * your_command
2 2 * * * your_command
# ...
21 21 * * * your_command
22 22 * * * your_command
23 23 * * * your_command
You can use this method which tells it to run every 61 minutes after the cron job.
while true
do
# do stuff here every 61 minutes
sleep 61m
done
Another option:
Cron can easily run every hour, but 61 minutes is harder to achieve.
The normal methods include using a sleep command or various rather
elaborate methods in the script itself to fire off every 61 minutes.
A much simpler method is using cron's cousin, the at command. The at
command will run through a file and run all the commands inside, so
you just need to place the commands in a file, one per line, then add
this line to the bottom of the file:
at now + 61 minutes < file
The commands can be any type of one-liner you want to use.
Here is an example. Call this file foo and to kick off the execution
the first time, you can simply run: sh foo
date >> ~/foo_out
cd ~/tmp && rm *
at now + 61 minutes < ~/foo
That will output the date and time to ~/foo_out then move to a tmp
directory and clean out files, then tell the at command to run itself
again in 61 minutes which will again run the at command after
executing the rest.

How can make cron job which happen at different hours and minutes

I want to make crontab where script occurs at different minutes for each hour like this
35 1,8,12,15,31 16,18,21 * * 0,1,2,3,4,5,6 python backup.py
I want script to run at 16hour and 31 minutes but it is giving me error bad hour
i want the cron occur at
1:35am , then 16:31, then 21:45
As there is not a pattern that can match the three times, it is not possible to schedule that just with one crontab expression. You will have to use three:
45 21 * * * python backup.py
31 16 * * * python backup.py
35 1 * * * python backup.py
Note also that python backup.py will probably not work. You have to define full path for both files and binaries:
35 1 * * * /usr/bin/python /your/dir/backup.py
Where /usr/bin/python or similar can be obtained with which python.
If the system which you are on has systemd, You can look into systemd timers(https://www.freedesktop.org/software/systemd/man/systemd.time.html). Then you might be able to achieve the randomness using the RandomizedDelaySec setting and an OnCalendar setting which will schedule the service to run every hour or interval you set plus will generate a RandomizedDelaySec at every run so that the interval is random.

Running a cron job at 2:30 AM everyday

How to configure a cron job to run every night at 2:30? I know how to make it run at 2, but not 2:30.
crontab -e
add:
30 2 * * * /your/command
To edit:
crontab -e
Add this command line:
30 2 * * * /your/command
Crontab Format:
MIN HOUR DOM MON DOW CMD
Format Meanings and Allowed Value:
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.
Restart cron with latest data:
service crond restart
As seen in the other answers, the syntax to use is:
30 2 * * * /your/command
# ^ ^
# | hour
# minute
Following the crontab standard format:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
It is also useful to use crontab.guru to check crontab expressions.
The expressions are added into crontab using crontab -e. Once you are done, save and exit (if you are using vi, typing :x does it). The good think of using this tool is that if you write an invalid command you are likely to get a message prompt on the form:
$ crontab -e
crontab: installing new crontab
"/tmp/crontab.tNt1NL/crontab":7: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n)
If you have further problems with crontab not running you can check Debugging crontab or Why is crontab not executing my PHP script?.
An easy way to write cron is to use the online cron generator
It will generate the line for you. One thing to note is that if you wish to run it each day (not just weekdays) you need to highlight all the days.
As an addition to the all above mentioned great answers, check the https://crontab.guru/ - a useful online resource for checking your crontab syntax.
What you get is human readable representation of what you have specified.
See the examples below:
30 2 * * * (answer of this question)
#daily
59 23 31 12 *
30 2 * * * wget https://www.yoursite.com/your_function_name
The first part is for setting cron job and the next part to call your function.
30 2 * * * Every Day at 2:30 Am
30-31 2 * * * Every Day at 2:30 -31 am
Along with he answers its important to understand the cron expressions , i face a lot of difficulty in understanding .
But an intuitive way to understand is given here .

Crontab is not working on Amazon EC2 server

Crontab is not working on Amazon EC2 Linux Server.
I have saved below codes in /etc/crontab file
crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
I have started crontab command already, but this one didn't work.
I also have saved this line in "crontab -e" too, but cron won't work.
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web
Is there anyone who had same experience like me?
Thank you.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user. For example:
0 12 * * * python3 example.py
In fact, specifying a user here prevented it from running.
Solved the problem.
I used this code and it works!
* 2 * * * root tar cvfpz /home/backup/web_`date +\%Y\%m\%d`.tar.gz /home/web
You should use crontab -e to create cron for the logged user, so that you don't need to inform the username.
See here: https://stackoverflow.com/a/16986464/1777152
For people who are dealing with AWS machines and EBS you need to specify the root keyword before the command since ec2-user isn't allowed to run crontabs. Of course there's a way to fix that.
you can edit the crontab by typing sudo nano /etc/cron.d/mycrontabs or crontab -e
* * * * * root bla bla
Also make sure e that the file is ended with a new line
Don't use nano, use the native sudo crontab -e command.

Running a cron job 3 times (1 pm, 2 pm and 3 pm for example)?

I am not sure how to run a cron job at 3 specific hours every day. I want to run it at 1pm, 2 pm and 3pm.
Is it possible, using a single expression?
you may use this:
# m h dom mon dow command
0 13,14,15 * * * /home/user/command
your /home/user/command will be run at 13:00, 14:00 and 15:00
As lenik stated, it can be done in single expression.
0 13,14,15 * * * <your-script-to-run>
Check this geedkstuff link for more examples
While the given answers are correct, an unexperienced user might not know where to put this expression. You have to edit the crontab file, like:
crontab -e
There you add
0 13,14,15 * * * /home/user/command
to execute your command at 13:00, 14:00 and 15:00. Also note that user has to be substituted with the user account the command is executed in.
You can try the following as well:
0 13-15 * * * /home/apps/sample.sh
To anyone landing here --> useful tool:
https://crontab.guru/
Please prefer range+step over commas:
Example: Run every 2h from 9h to 16h
m h dom mon dow command
0 9-16/2 * * * /home/user/command
Also applicable to minutes:
m h dom mon dow command
10-30/10 9-16/2 * * * /home/user/command
Crontab guru shows what it means, and the next scheduled jobs.
For example I typed this cron at 10h05:

Resources