0

I am trying to set a time IN SHELL SCRIPT so that it runs automatically during the working hours and stop for set times and rerun again after the set times. NOT run for set amount of time or period of time.. Time depends on how many things to process. Usually takes 3~5 hours.. but I want this program to pause at 12am to 3am and rerun again after that time..

for example.

if [ -d /tmp/test ] 
then 
    for dir in $(ls -d $DIR1/* | grep test)
    do
        ....
    done 
fi

how can I write this in Shell Script?

my point is to run a single shell script fully automated.. it runs only ONCE processing all the necessary work during this ONE run.. but at the given time it should PAUSE and rerun again.

how can I implement this inside of Shell Script?

some people recommended cron but isn't this only for execution command that does not go into the shell script? and isn't this also for just setting how long it should run?

Young
  • 43
  • 1
  • 11
  • It's a little unclear what this means: *it runs only ONCE processing all the necessary work during this ONE run.. but at the given time it should stop and rerun again.* Which says it should run just once, but then says it should rerun again. Maybe the conditions under which it does these things aren't clear. – lurker Mar 06 '15 at 14:28
  • Also, "for execution command that does not go into the shell script? " - Cron is a scheduler which will execute any given command at a given time or times - and a shell script can be a program in this case. Also cron has nothing to do with how long a program runs, it only executes commands. – asimovwasright Mar 06 '15 at 14:39
  • Perhaps what you are looking for is the Bash built-in **wait** or **sleep** commands. Wait can be used to pause the script until specific PID has completed, and sleep can be used to simply pause the script for a certain amount of time e.g.: **sleep 600** will pause the entire script for 10 minutes once the script reached that line. – asimovwasright Mar 06 '15 at 14:42
  • You can't control the time unless you have a warp-drive with speed 10+ :) /Really try formulate more precise definition of what do you need/. – clt60 Mar 06 '15 at 14:50
  • Perhaps OP is looking for the `at` tool? – Kevin Mar 06 '15 at 15:13
  • Sorry not STOP it should PAUSE and rerun.. so the SCRIPT dose not END until it finishes everything. it should ONLY PAUSE during the given time – Young Mar 06 '15 at 15:14
  • 2
    Write one script that does all your processing, without worrying about what time it is. Write another script that performs a `kill -STOP` and `kill -CONT` on your first script at appropriate times to pause and unpause it. – twalberg Mar 06 '15 at 19:23
  • See also http://stackoverflow.com/questions/11979061/bash-script-suspend-continue-a-process-within-script for an additional discussion of the approach suggested by @twalberg. +1 – tripleee Mar 09 '15 at 08:59
  • Can it happen that the script is started between 12am and 3am, or is it started only during the working hours? – Armali Sep 01 '15 at 11:13

1 Answers1

0

1) You description sounds a bit confusing, but, if I understand what your are asking, check the hour and if 'quiet time' then do a timer loop or perhaps something useful or simply exit.

2) As suggested by others, use cron to 'start' this every day at 3:01 AM - if it exits every day at midnight...

3) change your 'else' routine to simply exit

HOUR=$(date +%H)
if [[ ${HOUR} -ge 3 ]]
then
     go_do_your_stuff
else
     we_are_in_time_loop
fi
Dale_Reagan
  • 1,953
  • 14
  • 11