Skip to content
Advertisement

terminate a job after few seconds and process to next step in loop

Short question

Terminate a downloading command after few seconds

Long detailed

I have a file in which every line has a cron job written in such a way that if i parse over it line by line, then i will be able to run it manually.

somewhat like this

job1
job2
job3

i want to cross check if i’m able to run all this jobs or not, so i wrote a shell script, and i’m able to run each command. But the main problem is in case of successful command i’m not able to terminate it and move to next line, i need to wait for that job to complete

What i have tried so far

Method 1:

eval job1& sleep 3 ; kill -9 `ps -ef | grep "my process" | awk '{print $2}'`

Method 2:

eval job1& sleep 3 ; kill $! // not able to do this :(

Advertisement

Answer

You can use the timeout command for this purpose:

date; timeout 3 sleep 10; date
2018年  2月 22日 木曜日 17:27:34 JST
2018年  2月 22日 木曜日 17:27:37 JST

As you can see the sleep was stopped after 3 seconds.

You can define which signal has to be sent to the running command, by default the SIGTERM signal will be sent, and different duration formats are accepted: 10s, 1m, …

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement