Running crontab only on one line in a file each time - linux

I'm trying to configure crontab to execute at different times different lines of code inside a file. I basically have a bash script file that starts some java -jar. The problem is that each line should be executed at a different time. I can configure crontab to run the whole script at different times but no the lines to run. Now this is important that the bash file will stay only one file and not broken down to a few files.
Thanks!

One way of doing it (via command line arguments passed by cron)
some_script.sh:
if test $1 = 1 ; then
# echo "1 was entered"
java -jar some_file.jar
elif test $1 = 2 ; then
# echo "2 was entered"
java -jar another_file.jar
fi
crontab example:
* 1 * * * /bin/bash /home/username/some_script.sh 1
* 2 * * * /bin/bash /home/username/some_script.sh 2
Another approach (hour matching done in bash script)
some_script.sh:
hour=$(date +"%H");
if test $hour = 1 ; then
# echo "the hour is 1";
java -jar some_file.jar
elif test $hour = 2 ; then
# echo "the hour is 2";
java -jar another_file.jar
fi
crontab example:
* 1 * * * /bin/bash /home/username/some_script.sh
* 2 * * * /bin/bash /home/username/some_script.sh

Related

Crontab setting - execute script every 55 minutes

I found a interesting thing during creation of my crontab setting.
I used this command:
crontab -e
and fill this line:
*/55 * * * * export DISPLAY=:0 && /home/user/Documents/script.sh $2>/dev/null
My idea was create scheduler, which start script.sh every 55 minutes.
But this script is execute in this times (for example):
08:55, 09:00, 09:05, 09:55, 10:00, 10:05, ...
and I don't know why.
Can someone explain me that?
Replace the script like this and it should work.
*/5 * * * * [ $(( $(date +%s) / 60 % 55 )) -eq 0 ] && export DISPLAY=:0 && /home/user/Documents/script.sh $2>/dev/null
minute-hour-day-month-year
* any value
, value list separator
- range of values
/ step values
Another option is a self-replicating 'at' job. Only advantage over cron is that it is less obvious, and also if you needed it to kick off not every X minutes, but X minutes after the last job completed. So your script will just contain a line to create a new 'at' job before it exits. Something like:
echo "/full/path/to/my/script > /root/myScript.at.log" | at now + X minutes
so every 5 minutes it will do this:
number of seconds elapsed since 1. 1. 1970 will divided by 60 = how many minutes
echo $(date +%s)
1476201056 ... second
echo $(( $(date +%s) / 60 ))
24603351 ... minutes
after that it will use modulo on count of minutes
When result of modulo is 0, it will send TRUE value.
And it is a typical logical AND
[ $((......)) -eq 0 ] && export DISPLAY.. && .../script.sh
Thank you.
It is really helpful :)

Cronjob not running, attempting to echo string into file

My /etc/crontab looks like this:
* * * * * echo"Another 5 Minutes! " >> /tmp/5-minutes.txt
I figured this would append "Another 5 Minutes! " to /tmp/5-minutes.txt every minute. When I perform a 'less' command on the file, the changes are not there.
EDIT: If it helps, I have one other job in the crontab file, perhaps it's effecting the other job.
0 * * * * finger >> /tmp/hourly-finger.txt
* * * * * echo "Another 5 Minutes! " >> /tmp/5-minute.txt
Give a space after echo
echo "Another 5 Minutes! "
I figured it out. Because I'm editing the /etc/crontab directly I needed to specify a user for the crontab to run as. In my case I ran it as root so...
* * * * * root echo "Another 5 Minutes! : >> /tmp/5-minutes.txt

$ * * * * * echo "hi" => blado.kdb: command not found

I'm trying to set a cron job, namely echoing "hi" every minute.
When I do * * * * * echo "hi" I get blado.kdb: command not found. Any ideas how I could fix this?
Run crontab -e in your shell. This opens a text editor
Only then type * * * * * echo "hi". Save the file the text editor just opened for you
Your Cron task is now set
PS: echo "hi" will print "hi" in a void, if you want to see some results, set a task such as * * * * * touch /tmp/foo, and you'll see the modification date being updated every minute (ls -l /tmp/foo)

Creating a cronjob from a makefile?

I am trying to let my makefile setup a cronjob for my application. Unfortunately it appears to not be working as the $CRONENTRY variable seems to be empty. What am I doing wrong here?
addcron:
CRONENTRY="*/2 * * * * /usr/bin/node cronapp.js >> logfile.log"
crontab -l | { cat; echo ${CRONENTRY}; } | crontab -
Each command in a rule executes in its own subshell; variables do not survive from one command to the next. So if you want to use a variable this way, you have to string the commands together.
addcron:
CRONENTRY="whatever" ; \
do_something_with $(CRONENTRY)
What about
addcron:
CRONENTRY=
{ crontab -l; echo "*/2 * * * * /usr/bin/node cronapp.js >> logfile.log" } | crontab -
there you have one less pipe element...

How can I set cron to run certain commands every one and a half hours?

How can I set cron to run certain commands every one and a half hours?
That's not possible with a single expression in normal cron.
The best you could do without modifying the code is:
0 0,3,6,9,12,15,18,21 * * * [cmd]
30 1,4,7,10,13,16,19,22 * * * [cmd]
These might be compressible, depending on the version of cron you have to:
0 */3 * * * [cmd]
30 1-23/3 * * * [cmd]
Is there a good reason why you can't use 1 hour or 2 hours? It would be simpler for sure.
I haven't tried this personally, but you can find some info here on getting cron to run every 90 minutes: http://keithdevens.com/weblog/archive/2004/May/05/cron
An excert from the above link:
0 0,3,6,9,12,15,18,21 * * * <commands>
30 1,4,7,10,13,16,19,22 * * * <commands>
Two lines in the crontab. Along the lines of:
0 0,3,6,9,12,15,18,21 * * * /usr/bin/foo
30 1,4,7,10,13,16,19,22 * * * /usr/bin/foo
You could do it with two crontab entries. Each runs every three hours and they are offset by 90 minutes something like this:
0 0,3,6,9,12,15,18,21 * * *
30 1,4,7,10,13,16,19,22 * * *
*/10 * * * * root perl -e 'exit(time()%(90*60)>60)' && command
90 — it is one and a half hour in minutes
"> 60" — I give to cron ability to delay the start of script during a minute
Also with help of this hack you can set any period with a minute resolution
For example start the script every 71 minutes
* * * * * root perl -e 'exit(time()%(71*60)>60)' && command
You can achieve any frequency if you count the minutes(, hours, days, or weeks) since Epoch, add a condition to the top of your script, and set the script to run every minute on your crontab:
#!/bin/bash
minutesSinceEpoch=$(($(date +'%s / 60')))
# every 90 minutes (one and a half hours)
if [[ $(($minutesSinceEpoch % 90)) -ne 0 ]]; then
exit 0
fi
date(1) returns current date, we format it as seconds since Epoch (%s) and then we do basic maths:
# .---------------------- bash command substitution
# |.--------------------- bash arithmetic expansion
# || .------------------- bash command substitution
# || | .---------------- date command
# || | | .------------ FORMAT argument
# || | | | .----- formula to calculate minutes/hours/days/etc is included into the format string passed to date command
# || | | | |
# ** * * * *
$(($(date +'%s / 60')))
# * * ---------------
# | | |
# | | ·----------- date should result in something like "1438390397 / 60"
# | ·-------------------- it gets evaluated as an expression. (the maths)
# ·---------------------- and we can store it
And you may use this approach with hourly, daily, or monthly cron jobs:
#!/bin/bash
# We can get the
minutes=$(($(date +'%s / 60')))
hours=$(($(date +'%s / 60 / 60')))
days=$(($(date +'%s / 60 / 60 / 24')))
weeks=$(($(date +'%s / 60 / 60 / 24 / 7')))
# or even
moons=$(($(date +'%s / 60 / 60 / 24 / 656')))
# passed since Epoch and define a frequency
# let's say, every 7 hours
if [[ $(($hours % 7)) -ne 0 ]]; then
exit 0
fi
# and your actual script starts here
You could also use fcron which also accepts more complex time specifications such as :
# 01h30 my_cmd
#! /bin/sh
# Minute Cron
# Usage: cron-min start
# Copyright 2014 by Marc Perkel
# docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron"
# Free to use with attribution
# Run this script under Cron once a minute
basedir=/etc/cron-min
if [ $# -gt 0 ]
then
echo
echo "cron-min by Marc Perkel"
echo
echo "This program is used to run all programs in a directory in parallel every X minutes."
echo
echo "Usage: cron-min"
echo
echo "The scheduling is done by creating directories with the number of minutes as part of the"
echo "directory name. The minutes do not have to evenly divide into 60 or be less than 60."
echo
echo "Examples:"
echo " /etc/cron-min/1 # Executes everything in that directory every 1 minute"
echo " /etc/cron-min/5 # Executes everything in that directory every 5 minutes"
echo " /etc/cron-min/13 # Executes everything in that directory every 13 minutes"
echo " /etc/cron-min/90 # Executes everything in that directory every 90 minutes"
echo
exit
fi
for dir in $basedir/* ; do
minutes=${dir##*/}
if [ $(( ($(date +%s) / 60) % $minutes )) -eq 0 ]
then
for program in $basedir/$minutes/* ; do
if [ -x $program ]
then
$program &> /dev/null &
fi
done
fi
done
use "at" on crontab
the command is executed at midnight and then set to execute at 1.30
etc ..
0 */3 * * * echo "Command" | at now +90 minutes && Command
added the following to my crontab and is working
15 */1 * * * root /usr/bin/some_script.sh >> /tmp/something.log

Resources