Sponsored Content
Top Forums Shell Programming and Scripting Run a command once in three hours Post 303027151 by bakunin on Monday 10th of December 2018 08:05:32 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
AT(1)							      General Commands Manual							     AT(1)

NAME
at - execute commands at a later time SYNOPSIS
at time [ day ] [ file ] DESCRIPTION
At squirrels away a copy of the named file (standard input default) to be used as input to sh(1) at a specified later time. A cd(1) com- mand to the current directory is inserted at the beginning, followed by assignments to all environment variables. When the script is run, it uses the user and group ID of the creator of the copy file. The time is 1 to 4 digits, with an optional following `A', `P', `N' or `M' for AM, PM, noon or midnight. One and two digit numbers are taken to be hours, three and four digits to be hours and minutes. If no letters follow the digits, a 24 hour clock time is understood. The optional day is either (1) a month name followed by a day number, or (2) a day of the week; if the word `week' follows invocation is moved seven days further off. Names of months and days may be recognizably truncated. Examples of legitimate commands are at 8am jan 24 at 1530 fr week At programs are executed by periodic execution of the command /usr/lib/atrun from cron(8). The granularity of at depends upon how often atrun is executed. Standard output or error output is lost unless redirected. FILES
/usr/spool/at/yy.ddd.hhhh.uu activity to be performed at hour hhhh of year day ddd of year yy. uu is a unique number. /usr/spool/at/lasttimedone contains hhhh for last hour of activity. /usr/spool/at/past directory of activities now in progress /usr/lib/atrun program that executes activities that are due pwd(1) SEE ALSO
calendar(1), cron(8) DIAGNOSTICS
Complains about various syntax errors and times out of range. BUGS
Due to the granularity of the execution of /usr/lib/atrun, there may be bugs in scheduling things almost exactly 24 hours into the future. AT(1)
All times are GMT -4. The time now is 05:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy