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!
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.
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.
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!"
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With