Run a command once in three hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run a command once in three hours
# 1  
Old 12-10-2018
Run a command once in three hours

Hi All,

I have a bash script which is scheduled to run for every 20 minutes. Inside the bash script, one command which I am using need to be triggered only once in two or three hours.Is there anyway to achieve this.

For example,

Code:
if [ -f file ]
then
echo "hi"
else
echo "Hello"
UNIX Command---once in 3 hours
fi

In most cases file will not be there, so it will go to else portion and execute hello.. But this one I need to execute only once in 3 hours, instead of executing it every 20 minutes
# 2  
Old 12-10-2018
Scheduled by cron? On the exact hour, 20 min, and 40 min past? Or should it run only every ninth time the script is run?

And, what if the file is there when three hours are full? Execute the "Hi" and quit, i.e. NOT run the "UNIX Command"?
# 3  
Old 12-10-2018
Hi it should run only every 9th time the script is running.If the file is there yes we need to execute hi and quit
# 4  
Old 12-10-2018
Quote:
Originally Posted by ginrkf
Hi it should run only every 9th time the script is running.If the file is there yes we need to execute hi and quit
This is an example of what is called "inter-process communication" because one process (the program to be started) needs some information of another process (the last instance of the program and when it ran lastly). There are only so many means for such inter-process communication:

- semaphores
- shared memory
- message queues

In this case we need a "semaphore". Not a "UNIX semaphore" (in UNIX there is a certain OS feature implementing semaphores), but something alike. We simply put a file somewhere with the "UNIX date", the numbers of seconds passed since Jan 1st 1970, of when the program ran the last time:

Code:
#! /bin/ksh

typeset last="/tmp/myfile"

if [ -f file ] ; then
     echo "hi"
else
     echo "Hello"
     UNIX Command---once in 3 hours
     echo $(date +'%s') > "$last"
fi

exit 0

Now we just need to read this file if it is there (otherwise we are running for the first time) and make the starting of the program conditional to that. Notice that 10800 = 3x3600, 3 hours in seconds and 4294967295 is 2^^32-1:

Code:
#! /bin/ksh

typeset    last="/tmp/myfile"
typeset -i now=$(date +'$s')

if [ -f file ] ; then
     echo "hi"
else
     echo "Hello"
     if ! [ -f "$last" ] ; then
          echo "4294967295" > "$last"
     fi
     if (( now - 10800 <= $(cat $last) )) ; then
          UNIX Command---once in 3 hours
          echo $(date +'%s') > "$last"
     fi
fi

exit 0

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 12-10-2018
Thanks a lot
# 6  
Old 12-10-2018
Same principal approach as bakunin's, but mayhap a bit less resource consuming (no cat nor echo (i.e. opening time stamp file twice), just file system (meta data) operation, running date just once), given stat and date -d are available on your system:


Code:
if (( $(stat -c%Y $last) < $(date -d"3 hours ago" +%s) ))
 then  UNIX Command---once in 3 hours
       touch $last
fi

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help 'speeding' up this 'parsing' script - taking 24+ hours to run

Hi, I've written a ksh script that read a file and parse/filter/format each line. The script runs as expected but it runs for 24+ hours for a file that has 2million lines. And sometimes, the input file has 10million lines which means it can be running for more than 2 days and still not finish.... (9 Replies)
Discussion started by: newbie_01
9 Replies

2. Solaris

How to run cron entry every 5 min during office hours only?

Hi I need to setuop a cron entry to run every 5 min, only in office hours (between 8:00AM to 18:00PM, I did the following: 0,5,10,15,20,25,30,35,40,45,50,55 8,9,10,11,12,13,14,15,16,17,18 * * * /home/xxx/zzz.ksh But somehow does not work. Could it be wrong? (8 Replies)
Discussion started by: fretagi
8 Replies

3. UNIX for Dummies Questions & Answers

At command not running out of hours

Hi All, new to the forum and new to Unix but I have an issue which is annoying on a new level. I have included a short and full version for anyone needing more information. Short Version I am running a set of scripts that work and run fine. one of the scripts arranges the first... (4 Replies)
Discussion started by: Delboy4000
4 Replies

4. UNIX for Dummies Questions & Answers

Find command to get the modified files past 2 hours

Hello, How to get the modified/created files past 2 hours in Solaris with find command? Thank you. (7 Replies)
Discussion started by: balareddy
7 Replies

5. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

6. HP-UX

Crontab do not run on PM hours

Hi All I have a problem, I wonder if you can help me sort it out: I have the following entry in the cron: 00 1,13 * * * /home/report/opn_amt_gestores_credito.ksh > opn_amt_gestores_credito.log But the entry only runs at 01:07 I have stopped the cron deamon, and started, but it still... (39 Replies)
Discussion started by: fretagi
39 Replies

7. Shell Programming and Scripting

Command to clear logs for every 6 hours in solaris

Hi Folks, I need to remove log files for six hours on Solaris. before i used to do for every 24 hours below is the code for 1 day older log files, now i tried using -mmin +360 but it says command not found. Can someone please help me out!!! part of the code: LOG_FILE=`find /home/Logdir... (1 Reply)
Discussion started by: Sendhil.Kumaran
1 Replies

8. Shell Programming and Scripting

How to make a script run for a maximum of "x" number of hours only

How to make a script run for a maximum of "x" number of hours only (7 Replies)
Discussion started by: ScriptDummy
7 Replies

9. Shell Programming and Scripting

list the file created before 24 hours using ls command

I want to list the files created before past 24 hours using ls command. please help me on this (7 Replies)
Discussion started by: jayaramanit
7 Replies

10. UNIX for Dummies Questions & Answers

AT command to batch execute every x hours

I want to schedule a batch job (SQL)to run every 6 hours in Unix and the AT command syntax does not seem to cover this. I have created a file ncd_rpt in the directory report and have given the command in this file. at -f /report/ncd_rpt 1:00 am tomorrow I schedule this by running sh... (1 Reply)
Discussion started by: naveen79
1 Replies
Login or Register to Ask a Question