Need to write a shell script that starts one, then kills it, then starts another?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to write a shell script that starts one, then kills it, then starts another?
# 1  
Old 03-09-2016
Question Need to write a shell script that starts one, then kills it, then starts another?

This is on a CentOS box, I have two scripts that need to run in order.

I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which will self terminate once it's complete.

I'm unsure how to do the middle part, that is terminating the first script after a certain time limit, any suggestions?
# 2  
Old 03-09-2016
You can start the first program in a subshell background process, record the process id and use the sleep command to wait x number of hours. Then you can use the process id to kill the program, after which you can start the second one...

Last edited by Scrutinizer; 03-09-2016 at 09:04 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-09-2016
Subshell? I think you mean a background process.
Killing a background process ID $! is tricky; e.g. the PID can have been re-assigned to another program in the meantime.
Job control is better:
Code:
script1 &
sleep 3600
kill %1 2>/dev/null
script2

In CentOS 6 there is /usr/bin/timeout, that will immediately run script2 when script1 has terminated.
Code:
timeout 3600 script1
script2

# 4  
Old 03-09-2016
Yes I meant a background process, corrected in my post..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

When I run the script, the cursor starts on the wrong line?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: It's a shell script using a looping logic, trap, tput, if, while. Most of the scripts in this book aren't written... (2 Replies)
Discussion started by: ckleinholz
2 Replies

2. UNIX for Advanced & Expert Users

Script to rename file that was generated today and which starts with date

hello, can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file. here is example. # find . -type f -mtime -1 ./20130529_4995733057260357019.xml # this finename should be renamed to this format.... (6 Replies)
Discussion started by: bobby320
6 Replies

3. Shell Programming and Scripting

Detect if script starts from queue

Dear community, what I'm try to do is deny users to run a script without parameters from command bash, but the same script should run without parameters only from crontab. Example runs by crontab:*/5 * * * * /tmp/script.sh Here the normal execution starts every 5 minutes Example #1 runs by... (16 Replies)
Discussion started by: Lord Spectre
16 Replies

4. Shell Programming and Scripting

Check if string starts with $ symbol

How do I check that a string $AA22CC3 starts with the "$" symbol ? I have tried : checksum='$AAB3E45' echo $checksum case $checksum in $* ) echo success ; esac Thanks ... (4 Replies)
Discussion started by: cillmor
4 Replies

5. Shell Programming and Scripting

Start script when a user starts a remote session

Howdy, I'm fairly new at bash scripting, but (for some reason) I've been tasked with building a bastion server and logging all (ssh/telnet) remote activity. Each session must create a unique log file - the name of each file must include the user ID, the connection method (ssh/telnet), the name... (2 Replies)
Discussion started by: kilo90
2 Replies

6. UNIX for Dummies Questions & Answers

Why do a unix script starts with a comment?

For eg: #!/usr/bin/ksh <remaining code goes here> .. .. Does the compiler ignores that? Thanks (2 Replies)
Discussion started by: ajincoep
2 Replies

7. UNIX for Dummies Questions & Answers

Display lines not starts with #

hiiiii $ grep ^"#" $file Will give the lines , which starts with # .And I wanna get the lines which are not starting with #. How to implement that. Thanking you Krish:b: (10 Replies)
Discussion started by: krishnampkkm
10 Replies

8. Red Hat

Firefox do not starts from Panel

Hi I am facing a problem related firefox and some other applications like DocumentViewer (evince). I can run the firefox from terminal by just giving command "firefox" but when I made its shortcut in panel and try to run through that panel shortcut , the mouse coursor just becomes busy for... (10 Replies)
Discussion started by: mak_mailbox
10 Replies

9. Shell Programming and Scripting

Delete lines that starts with a certain letter

How can I delete those lines that starts with a certain letter? abc def ghi xyz abc def ace gik moq abe imq gxm I want to delete the line that starts with "x". Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. UNIX for Dummies Questions & Answers

filename that starts with a space

I accidentally started a filename with a spce and I can not get rid of it. Any words of advice? (6 Replies)
Discussion started by: noobie_doo
6 Replies
Login or Register to Ask a Question