Execute the first line from last

Hi,

I have a script xyz.sh and in this, following lines exists.

#!/bin/bash

./abc.sh PB
sleep 60
./abc.sh RA
sleep 60
./abc.sh GS
sleep 60
./abc.sh S
sleep 60
./abc.sh SI
sleep 60
./abc.sh DR
sleep 12000
./abc.sh CT
sleep 7200
./abc.sh DL
sleep 60
./abc.sh DE
sleep 68400

How to execute the first line " ./abc.sh PB" after last line. It should keep on repeating.

You need a loop. ( this is an infinite loop )

while :
do
  ##do anything here
done
(tail -1) 

will give u the last line. i am not sure hw can u execute it.
u can use exec command and try executing that way it wil help. and if ur sure the last line has a sleep command then u can do.

exec `cat xyz.ksh | tail -n` 

i am not very familiar with use of exec, just searched it on net. i think this will work. on my system when i execute this command. this is closing my window. so not sure if i m correect or not.

sorry i didnt read the last line.

I should keep on the executing the script.
After sleep 68400, I should again call the ./abc.sh PB and next line and so on.
Execution should be something recursive. How can I do that

As I mentioned,

#!/bin/bash

while :
do
 ./abc.sh PB
 sleep 60
 ./abc.sh RA
 sleep 60
 ./abc.sh GS
 sleep 60
 ./abc.sh S
 sleep 60
 ./abc.sh SI
 sleep 60
 ./abc.sh DR
 sleep 12000
 ./abc.sh CT
 sleep 7200
 ./abc.sh DL
 sleep 60
 ./abc.sh DE
 sleep 68400
done

then what anchal is saying will work awesomely.

sandy do u wnt to call the same script in cronjob also?
then copy whole of ur script inside the while loop as shown above and it will run for like 4ever unless and untill some1 kills it. or system crashes :stuck_out_tongue:

Hi, I would use cron for that, e.g. (start timees by approximation, first job starting at 01:01 hours):

echo " 1  1 * * *  /path/to/abc.sh PB
 2  1 * * *  /path/to/abc.sh RA
 3  1 * * *  /path/to/abc.sh GS
 4  1 * * *  /path/to/abc.sh S
 5  1 * * *  /path/to/abc.sh SI
 6  1 * * *  /path/to/abc.sh DR
26  4 * * *  /path/to/abc.sh CT
26  6 * * *  /path/to/abc.sh DL
27  6 * * *  /path/to/abc.sh DE" | crontab

If you are not allowed to use cron, then look into nohup, e.g.:

nohup /path/to/xyz.sh &

Hi Scrutinizer,
I just tried your method to check what happens, and I am receiving this.

$ echo " 1  1 * * *  /path/to/abc.sh PB
 2  1 * * *  /path/to/abc.sh RA
 3  1 * * *  /path/to/abc.sh GS
 4  1 * * *  /path/to/abc.sh S
 5  1 * * *  /path/to/abc.sh SI
 6  1 * * *  /path/to/abc.sh DR
26  4 * * *  /path/to/abc.sh CT
26  6 * * *  /path/to/abc.sh DL
27  6 * * *  /path/to/abc.sh DE" | crontab
crontab: usage error: file name must be specified for replace
usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
$ 

Is there something wrong which I am doing?

$ uname
CYGWIN_NT-5.1
$ 

I think your particular crontab cannot read from stdin. Just redirect the echo to a temporary file and then use:

crontab tmpfile
1 Like

ok.
Yes this worked.
Thank you.

I just looked at the man page for the possible options and found.
like usual way, in my crontab I need to provide "-" if the input is stdin.

SYNOPSIS
       crontab [-u user] file


       The  first  form  of  this command is used to install a new crontab from some named file or standard input if the
       pseudo-filename ``-'' is given.

So, this worked for me.

$ echo " 1  1 * * *  /path/to/abc.sh PB
 2  1 * * *  /path/to/abc.sh RA
 3  1 * * *  /path/to/abc.sh GS
 4  1 * * *  /path/to/abc.sh S
 5  1 * * *  /path/to/abc.sh SI
 6  1 * * *  /path/to/abc.sh DR
26  4 * * *  /path/to/abc.sh CT
26  6 * * *  /path/to/abc.sh DL
27  6 * * *  /path/to/abc.sh DE" | crontab -
$ 

Thanks a lot for the info.