Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run bash script from another script and exit first script while second is running

Tags:

linux

bash

First bash script is:

./second #takes too long

I want kill first bash script while second bash script is still running.

Can I do that?

UPDATE

First script run by cronjob!

like image 440
Daniyal Avatar asked Dec 23 '14 19:12

Daniyal


People also ask

Can I change Bash script while running?

with the current version of bash, modifying a script on-disk while it is running will cause bash to "try" to load the changes into memory and take these on in the running script. if your changes come after the currently executing line, the new lines will be loaded and executed.

How do you run multiple scripts one after another but only after previous one got completed?

Using wait. We can launch a script in the background initially and later wait for it to finish before executing another script using the wait command. This command works even if the process exits with a non-zero failure code.

How do I exit Bash script early?

To add a conditional statement and exit a for loop early, use a break statement. The following code shows an example of using a break within a for loop: #!/bin/bash for i in {1.. 10} do if [[ $i == '2' ]] then echo "Number $i!" break fi echo "$i" done echo "Done!"


2 Answers

If the last thing that the first script does is to call the second script, then do this:

exec ./second

which replaces the first script's process with the second.

otherwise

nohup ./second &
disown
like image 140
glenn jackman Avatar answered Sep 28 '22 04:09

glenn jackman


For Linux you would use the "kill" command after obtaining the PID of the program you wish to exit-

http://linux.about.com/library/cmd/blcmdl_kill.htm

I don't know how you would get the PID automatically via a script however.

It appears you can also use "pkill" to terminate processes by name-

Ex-

pkill -9 ping

Or killall-

killall firefox

https://www.digitalocean.com/community/tutorials/how-to-use-ps-kill-and-nice-to-manage-processes-in-linux

The answer below applies to BATCH files, not bash. I read the question wrong.

You can close an instance by getting the PID. You can also use this bit of code-

taskkill /f /im "x"

Where "x" is the name of the program you wish to close, as it would appear in the Task Manager.

Explanation of the parameters I gave-

/im ImageName : Specifies the image name of the process to be terminated. Use the wildcard (*) to specify all image names.

/f : Specifies that process(es) be forcefully terminated. This parameter is ignored for remote processes; all remote processes are forcefully terminated.

More info- http://technet.microsoft.com/en-us/library/bb491009.aspx

like image 34
Brady Avatar answered Sep 28 '22 04:09

Brady