I'm pretty new to shell scripts but I have some binaries I'd like to run in a loop over a set of different input files. I need them to be run in sequence: repeat after current one complete.

I got as far as trying to run two process in separate terminals and saving the pid but when I go to kill the process, it looks like the pids have changed.

gnome-terminal -e "vi" & MAIN_PID=$!
gnome-terminal -e "gedit" & SIM_PID=$!
echo "MAIN Process ID is " $MAIN_PID
echo "SIM Process ID is " $SIM_PID
sudo kill $MAIN_PID

Terminal Output:

MAIN Process ID is  8532
SIM Process ID is  8542
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
kill: (8532): No such process
user:~/Desktop$ ps -A | grep vi
 1026 ?        00:00:01 dconf-service
 8541 pts/3    00:00:00 vi
user:~/Desktop$ ps -A | grep gedit
 8550 pts/4    00:00:00 gedit

If there's an easier way to get what I want started, I'd like to know as well. I was planning to start these processes and check some condition to know when they have completed, kill the process and repeat with different set of inputs.


When you run gnome-terminal -e "vi" & MAIN_PID=$!, you are not capturing the PID of vi in MAIN_PID; you are capturing the PID of that instance of gnome-terminal. If you want to find the PID of a process owned by gnome_terminal, you could do some magic on the output of pstree:

pstree -p $MAIN_PID | grep -o 'vim([0-9]*) | grep -o '[0-9]*'

To check whether a process still exists, you can use kill -0 which will not send any signals, and either return with an exit code of 0 if the process exists, or 1 if it does not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.