Cronjob not running, attempting to echo string into file - linux

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

Related

Offset cron job for only one instance? [duplicate]

Is it possible to have a cronjob run every 15 minutes (over every hour etc..) except for at 3AM?
I have another special cronjob I want to run at 3AM, but I don't want the other one to run at the same time...
With one cron line, no. With three, yes:
# Every 15 minutes except for 3:00-3:59
*/15 0-2,4-23 * * * thejob
# 3:15, 3:30, 3:45
15-45/15 3 * * * thejob
# 3:00 dead
0 3 * * * otherjob
I made my own solution, but I wanted to see what other people thought of!
I put this on the top of my desired script. I wanted it to not run at the half hour either so it doesn't do it on both.
On top of the script:
if [ $(date +%M) = 00 ] || [ $(date +%M) = 30 ]
then
exit
fi
The cron line:
*/15 * * * * ~/path/to/file
Hope anyone uses my solution too.
0,15,30,45 0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * * your cron job

Running crontab only on one line in a file each time

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

$ * * * * * 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)

Crontab run every 15 minutes except at 3AM?

Is it possible to have a cronjob run every 15 minutes (over every hour etc..) except for at 3AM?
I have another special cronjob I want to run at 3AM, but I don't want the other one to run at the same time...
With one cron line, no. With three, yes:
# Every 15 minutes except for 3:00-3:59
*/15 0-2,4-23 * * * thejob
# 3:15, 3:30, 3:45
15-45/15 3 * * * thejob
# 3:00 dead
0 3 * * * otherjob
I made my own solution, but I wanted to see what other people thought of!
I put this on the top of my desired script. I wanted it to not run at the half hour either so it doesn't do it on both.
On top of the script:
if [ $(date +%M) = 00 ] || [ $(date +%M) = 30 ]
then
exit
fi
The cron line:
*/15 * * * * ~/path/to/file
Hope anyone uses my solution too.
0,15,30,45 0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * * your cron job

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