Linux: run a command every 50 minutes randomly - linux

I need to run a curl request to locahost at least once in every 30 mins. So the command will be curl http://localhost:8080.
The catch here is, I want to select a time randomly between 5min - 30 min and then execute the curl command. The pseudo-code might look like this
while(true)
n = random number between 5-30
run curl http://localhost:8080 after 'n' minutes
A detailed answer would be nice since I don't have much knowledge about linux.

while true; do
sleep $(((RANDOM%25+5)*60))
curl http://localhost:8080
done

If you run above script, you must run as background process and make sure it will not be killed by something (OS, other users,...)
Another way is use cronjob to trigger automatically but the script is more complex.
Cronjob setting:
* * * * * bash test_curl.sh >> log_file.log
Shell script test_curl.sh:
#!/bin/bash
# Declare some variable
EXECUTE_TIME_FILE_PATH="./execute_time"
# load expected execute time
EXPECTED_EXECUTE_TIME=$(cat $EXECUTE_TIME_FILE_PATH)
echo "Start at $(date)"
# calculate current time and compare with expected execute time
CURRENT_MINUTE_OF_TIME=$(date +'%M')
if [[ "$EXPECTED_EXECUTE_TIME" == "$CURRENT_MINUTE_OF_TIME" ]];
then
curl http://localhost:8080
# Random new time from 5 -> 30
NEXT_RANDOM=$((RANDOM%25+5))
# Get current time
CURRENT_TIME=$(date +'%H:%M')
# Calculate next expected execute time = Current Time + Next Random
NEXT_EXPECTED_EXECUTE_TIME=$(date -d "$CURRENT_TIME $NEXT_RANDOM minutes" +'%M')
# Save to file
echo -n $NEXT_EXPECTED_EXECUTE_TIME > $EXECUTE_TIME_FILE_PATH
echo "Next Executed time is $(date -d "$CURRENT_TIME $NEXT_RANDOM minutes" +'%H:%M')"
else
echo "This $(date +'%H:%M') is not expected time to run test"
fi
echo "End at $(date)"
I commented out in line so you can read it easily.
**
Update: Importance: file execute_time must has initial value. For
example, the current minute of first time you execute.
**

Related

Advice with Bash script to perform actions on certain conditions

I have written part of a small (hopefully) simple script that checks a URL for an error being returned using wget. It then outputs the error to a log file for alerting purposes. What I then want to do is for a service to be restarted automatically.
I will be running this check via a cronjob every minute, so if there is still an error after the service has been restarted already, I don't want the script to restart the service again.
Is there a elegant way to achieve this?
This is what I have so far, a wget check, if error code 5, output to the health.log file and restart nginx, however, I don't want this looping around restarting nginx every 60 seconds when running on a cronjob.
#!bin/bash
URL='http://some-url-here/'
LOG='/var/log/nginx/health.log'
wget -q $URL
if [ $? = 5 ] ; then
echo "$(date). SSL Error." > $LOG
sudo service nginx restart
exit
fi
Assumptions:
it's ok if we create a new file (restart.log), otherwise we could append a new line to $LOG
we'll only perform a restart attempt every 10 minutes (aka 600 seconds)
OP wants to append to the current $LOG (current code overwrites/replaces the entire file each time the script runs)
Proposed method:
use a new file to store the epoch time at which the last restart was attempted
before trying a restart we compare the current epoch with the saved epoch and only proceed (with the restart) if the difference in epochs is greater than 600 seconds
Modifying OP's current script:
#!/bin/bash # add "/" at start of shebang path
URL='http://some-url-here/'
LOG='/var/log/nginx/health.log'
RLOG='/var/log/nginx/restart.log'
touch "$RLOG"
wget -q $URL
if [ $? = 5 ] ; then
echo "$(date). SSL Error." >> "$LOG" # replace ">" with ">>" so that we append to $LOG
read -r prev_epoch < <(tail -1 "$RLOG") # retrieve epoch of last restart attempt
prev_epoch="${prev_epoch:-0}" # default to 0 if there is nothing in the file
printf -v curr_epoch '%(%s)T' # use printf builtin to grab current epoch and save in variable 'curr_epoch'
# curr_epoch=$((date '+%s')) # uncomment if 'printf -v' is not available in your system
delta=$((curr_epoch - prev_epoch))
if [[ "${delta}" -gt 600 ]] ; then
sudo service nginx restart
echo "${curr_epoch}" > "$RLOG" # replace ">" with ">>" if you want to maintain a history of restart epochs; the "tail -1" should insure we only grab the 'last' entry
exit
else
echo "it has only been ${delta} seconds since last restart attempt; skipping restart" >> "$LOG"
fi
fi

Is there a way to get the duration time for a running cronjob?

I want to monitor the long running time cronjob.
enter image description here
You could do:
#!/bin/bash
echo "Starting job blablabla" >>/tmp/job.log
date >>/tmp/duration.log
# THE REST OF THE CODE FOR SCRIPT blablabla
echo "End of job blablabla" >>/tmp/duration.log
date >>/tmp/duration.log
Every execution of the script will add entries with start and end time
Or directly in the cron:
* * * * * time -o /tmp/timing.txt blablabla.sh >/tmp/blablabla.out 2>/tmp/blablabla.out2
time -o shows the resource usage duration for the command.
The -o option specifies which file to send the results into.
The > and 2> redirections are to keep logs of the output and errors. But that is not related to time itself, just a good idea all around with cron jobs.

How to check last running time of any script in linux instance

I want to check if my scripts ran the last night(or last ran timestamp) on linux instance based on scripts crontab running time stamp.
So how to get scripts last ran time on linux instance?
I would suggest better record the start time during the start of the script and end time at the end of the Script.
# Start Time Entry
echo "Start : " $(date +%T) > exec.log
start=`date +%s`
CALL YOUR SCRIPT HERE
# End Time Entry
end=`date +%s`
echo "End : " $(date +%T) >> exec.log
# Get the Runtime
runtime=$((end-start))
echo "Runtime: $runtime" >> exec.log
If there is any better way, I am also curious to see and implement too.
grep cron from your "messages" or "syslog
grep -i cron /var/log/messages
or create a separate log file for cron from rsyslog, edit file /etc/rsyslog.conf and change #cron to cron. You will find logs in /var/log/cron

Linux - Run script after time period expires

I have a small NodeJS script that does some processing. Depending on the amount of data needing to be processed, this can take a couple of seconds to hours.
What I want is to do is schedule this command to run every hour after the previous attempt has completed. I'm wary of using something like cron because I need to ensure that two instances of the script aren't running at the same.
If you really don't like cron (or at) you can just use a simple bash script:
#!/bin/bash
while true
do
#Do something
echo Invoke long-running node.js script
#Wait an hour
sleep 3600
done
The (obvious) drawback is that you will have to make it run in background somehow (i.e. via nohup or screen) and add a proper error handling (taking that you script might fail, and you still want it to run again in an hour).
A bit more elaborate "custom script" solution might be like that:
#!/bin/bash
#Settings
LAST_RUN_FILE=/var/run/lock/hourly.timestamp
FLOCK_LOCK_FILE=/var/run/lock/hourly.lock
FLOCK_FD=100
#Minimum time to wait between two job runs
MIN_DELAY=3600
#Welcome message, parameter check
if [ -z $1 ]
then
echo "Please specify the command (job) to run, as follows:"
echo "./hourly COMMAND"
exit 1
fi
echo "[$(date)] MIN_DELAY=$MIN_DELAY seconds, JOB=$#"
#Set an exclusive lock, or skip execution if it is already set
eval "exec $FLOCK_FD>$FLOCK_LOCK_FILE"
if ! flock -n $FLOCK_FD
then
echo "Lock is already set, skipping execution."
exit 0
fi
#Last run timestamp
if ! [ -e $LAST_RUN_FILE ]
then
echo "Timestamp file ($LAST_RUN_FILE) is missing, creating a new one."
echo 0 >$LAST_RUN_FILE
fi
#Compute delay, and wait
let DELAY="$MIN_DELAY-($(date +%s)-$(cat $LAST_RUN_FILE))"
if [ $DELAY -gt 0 ]
then
echo "Waiting for $DELAY seconds, before proceeding..."
sleep $DELAY
fi
#Proceed with an actual task
echo "[$(date)] Running the task..."
echo
"$#"
#Update the last run timestamp
echo
echo "Done, going to update the last run timestamp now."
date +%s >$LAST_RUN_FILE
This will do 2 things:
Set an exclusive execution lock (with flock), so that no two instances of the job will run at the same time, irregardless of how you start them (manually or via cron e.t.c.);
If the last job was completed less then MIN_DELAY seconds ago,
it will sleep for the remaining time, before running the job again;
Now, if you schedule this script to run, say every 15 minutes with cron, like that:
* * * * * /home/myuser/hourly my_periodic_task and it's arguments
It will be guaranteed to execute with the fixed delay of at least MIN_DELAY (one hour) since the last job completed, and any intermediate runs will be skipped.
In the worst case, it will execute in MIN_DELAY + 15 minutes,
(as the scheduling period is discrete), but never earlier than that.
Other non-cron scheduling methods should work too (i.e. just running this script in a loop, or re-scheduling and each run with at).
You can use a cron and add process.exit(0) to your node script

Test a weekly cron job [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have a #!/bin/bash file in cron.week directory.
Is there a way to test if it works? Can't wait 1 week
I am on Debian 6 with root
Just do what cron does, run the following as root:
run-parts -v /etc/cron.weekly
... or the next one if you receive the "Not a directory: -v" error:
run-parts /etc/cron.weekly -v
Option -v prints the script names before they are run.
A wee bit beyond the scope of your question... but here's what I do.
The "how do I test a cron job?" question is closely connected to "how do I test scripts that run in non-interactive contexts launched by other programs?" In cron, the trigger is some time condition, but lots of other *nix facilities launch scripts or script fragments in non-interactive ways, and often the conditions in which those scripts run contain something unexpected and cause breakage until the bugs are sorted out. (See also: https://stackoverflow.com/a/17805088/237059 )
A general approach to this problem is helpful to have.
One of my favorite techniques is to use a script I wrote called 'crontest'. It launches the target command inside a GNU screen session from within cron, so that you can attach with a separate terminal to see what's going on, interact with the script, even use a debugger.
To set this up, you would use "all stars" in your crontab entry, and specify crontest as the first command on the command line, e.g.:
* * * * * crontest /command/to/be/tested --param1 --param2
So now cron will run your command every minute, but crontest will ensure that only one instance runs at a time. If the command takes time to run, you can do a "screen -x" to attach and watch it run. If the command is a script, you can put a "read" command at the top to make it stop and wait for the screen attachment to complete (hit enter after attaching)
If your command is a bash script, you can do this instead:
* * * * * crontest --bashdb /command/to/be/tested --param1 --param2
Now, if you attach with "screen -x", you'll be facing an interactive bashdb session, and you can step through the code, examine variables, etc.
#!/bin/bash
# crontest
# See https://github.com/Stabledog/crontest for canonical source.
# Test wrapper for cron tasks. The suggested use is:
#
# 1. When adding your cron job, use all 5 stars to make it run every minute
# 2. Wrap the command in crontest
#
#
# Example:
#
# $ crontab -e
# * * * * * /usr/local/bin/crontest $HOME/bin/my-new-script --myparams
#
# Now, cron will run your job every minute, but crontest will only allow one
# instance to run at a time.
#
# crontest always wraps the command in "screen -d -m" if possible, so you can
# use "screen -x" to attach and interact with the job.
#
# If --bashdb is used, the command line will be passed to bashdb. Thus you
# can attach with "screen -x" and debug the remaining command in context.
#
# NOTES:
# - crontest can be used in other contexts, it doesn't have to be a cron job.
# Any place where commands are invoked without an interactive terminal and
# may need to be debugged.
#
# - crontest writes its own stuff to /tmp/crontest.log
#
# - If GNU screen isn't available, neither is --bashdb
#
crontestLog=/tmp/crontest.log
lockfile=$(if [[ -d /var/lock ]]; then echo /var/lock/crontest.lock; else echo /tmp/crontest.lock; fi )
useBashdb=false
useScreen=$( if which screen &>/dev/null; then echo true; else echo false; fi )
innerArgs="$#"
screenBin=$(which screen 2>/dev/null)
function errExit {
echo "[-err-] $#" | tee -a $crontestLog >&2
}
function log {
echo "[-stat-] $#" >> $crontestLog
}
function parseArgs {
while [[ ! -z $1 ]]; do
case $1 in
--bashdb)
if ! $useScreen; then
errExit "--bashdb invalid in crontest because GNU screen not installed"
fi
if ! which bashdb &>/dev/null; then
errExit "--bashdb invalid in crontest: no bashdb on the PATH"
fi
useBashdb=true
;;
--)
shift
innerArgs="$#"
return 0
;;
*)
innerArgs="$#"
return 0
;;
esac
shift
done
}
if [[ -z $sourceMe ]]; then
# Lock the lockfile (no, we do not wish to follow the standard
# advice of wrapping this in a subshell!)
exec 9>$lockfile
flock -n 9 || exit 1
# Zap any old log data:
[[ -f $crontestLog ]] && rm -f $crontestLog
parseArgs "$#"
log "crontest starting at $(date)"
log "Raw command line: $#"
log "Inner args: $#"
log "screenBin: $screenBin"
log "useBashdb: $( if $useBashdb; then echo YES; else echo no; fi )"
log "useScreen: $( if $useScreen; then echo YES; else echo no; fi )"
# Were building a command line.
cmdline=""
# If screen is available, put the task inside a pseudo-terminal
# owned by screen. That allows the developer to do a "screen -x" to
# interact with the running command:
if $useScreen; then
cmdline="$screenBin -D -m "
fi
# If bashdb is installed and --bashdb is specified on the command line,
# pass the command to bashdb. This allows the developer to do a "screen -x" to
# interactively debug a bash shell script:
if $useBashdb; then
cmdline="$cmdline $(which bashdb) "
fi
# Finally, append the target command and params:
cmdline="$cmdline $innerArgs"
log "cmdline: $cmdline"
# And run the whole schlock:
$cmdline
res=$?
log "Command result: $res"
echo "[-result-] $(if [[ $res -eq 0 ]]; then echo ok; else echo fail; fi)" >> $crontestLog
# Release the lock:
9<&-
fi
After messing about with some stuff in cron which wasn't instantly compatible I found that the following approach was nice for debugging:
crontab -e
* * * * * /path/to/prog var1 var2 &>>/tmp/cron_debug_log.log
This will run the task once a minute and you can simply look in the /tmp/cron_debug_log.log file to figure out what is going on.
It is not exactly the "fire job" you might be looking for, but this helped me a lot when debugging a script that didn't work in cron at first.
I'd use a lock file and then set the cron job to run every minute. (use crontab -e and * * * * * /path/to/job) That way you can just keep editing the files and each minute they'll be tested out. Additionally, you can stop the cronjob by just touching the lock file.
#!/bin/sh
if [ -e /tmp/cronlock ]
then
echo "cronjob locked"
exit 1
fi
touch /tmp/cronlock
<...do your regular cron here ....>
rm -f /tmp/cronlock
What about putting it into cron.hourly, waiting until the next run of hourly cron jobs, then removing it? That would run it once within an hour, and in the cron environment. You can also run ./your_script, but that won't have the same environment as under cron.
Aside from that you can also use:
http://pypi.python.org/pypi/cronwrap
to wrap up your cron to send you an email upon success or failure.
None of these answers fit my specific situation, which was that I wanted to run one specific cron job, just once, and run it immediately.
I'm on a Ubuntu server, and I use cPanel to setup my cron jobs.
I simply wrote down my current settings, and then edited them to be one minute from now. When I fixed another bug, I just edited it again to one minute from now. And when I was all done, I just reset the settings back to how they were before.
Example: It's 4:34pm right now, so I put 35 16 * * *, for it to run at 16:35.
It worked like a charm, and the most I ever had to wait was a little less than one minute.
I thought this was a better option than some of the other answers because I didn't want to run all of my weekly crons, and I didn't want the job to run every minute. It takes me a few minutes to fix whatever the issues were before I'm ready to test it again. Hopefully this helps someone.
The solution I am using is as follows:
Edit crontab(use command :crontab -e) to run the job as frequently
as needed (every 1 minute or 5 minutes)
Modify the shell script which should be executed using cron to prints the output into some file (e.g: echo "Working fine" >>
output.txt)
Check the output.txt file using the command : tail -f output.txt, which will print the latest additions into this file, and thus you can track the execution of the script
I normally test by running the job i created like this:
It is easier to use two terminals to do this.
run job:
#./jobname.sh
go to:
#/var/log and run
run the following:
#tailf /var/log/cron
This allows me to see the cron logs update in real time. You can also review the log after you run it, I prefer watching in real time.
Here is an example of a simple cron job. Running a yum update...
#!/bin/bash
YUM=/usr/bin/yum
$YUM -y -R 120 -d 0 -e 0 update yum
$YUM -y -R 10 -e 0 -d 0 update
Here is the breakdown:
First command will update yum itself and next will apply system updates.
-R 120 : Sets the maximum amount of time yum will wait before performing a command
-e 0 : Sets the error level to 0 (range 0 - 10). 0 means print only critical errors about which you must be told.
-d 0 : Sets the debugging level to 0 - turns up or down the amount of things that are printed. (range: 0 - 10).
-y : Assume yes; assume that the answer to any question which would be asked is yes
After I built the cron job I ran the below command to make my job executable.
#chmod +x /etc/cron.daily/jobname.sh
Hope this helps,
Dorlack
sudo run-parts --test /var/spool/cron/crontabs/
files in that crontabs/ directory needs to be executable by owner - octal 700
source: man cron and NNRooth's
I'm using Webmin because its a productivity gem for someone who finds command line administration a bit daunting and impenetrable.
There is a "Save and Run Now" button in the "System > Scheduled Cron Jobs > Edit Cron Job" web interface.
It displays the output of the command and is exactly what I needed.

Resources