Karric
Karric

Reputation: 1565

Terminal - Close all terminal windows/processes

I have a couple cli-based scripts that run for some time.

I'd like another script to 'restart' those other scripts.

I've checked SO for answers, but the scenarios were not applicable enough to mine, as I'm trying to end Terminal processes using Terminal.

Process:

  1. 2 cli-based scripts are running (node, python, etc).
  2. 3rd script is run and decides whether or not to restart the other 2. This can't quit Terminal, but has to end current processes.
  3. 3rd script then runs an executable that restarts everything.

Currently none of the terminal windows are named, and from reading the other posts, I can see that it may be helpful to do so.

I can mostly set this up, I just could not find a command that would end all other terminal processes and close them.

Upvotes: 1

Views: 4629

Answers (2)

Frantisek Hallo
Frantisek Hallo

Reputation: 1711

The question headline is quite general, so is my reply killall bash

or generically killall processName

eg. killall chrome

Upvotes: 0

tk421
tk421

Reputation: 5957

There are a couple of ways to do this. Most common is having a pidfile. This file contains the process ID (pid) of the job you want to kill later on. A simple way to create the pidfile is:

$ node server &
$ echo $! > /tmp/node.pidfile

$! contains the pid of the process that was most recently backgrounded.

Then later on, you kill it like so:

$ kill `cat /tmp/node.pidfile`

You would do similar for the python script.

The other less robust way is to do a killall for each process and assume you are not running similar node or python jobs.

Refer to What is a .pid file and what does it contain? if you're not familiar with this.

Upvotes: 1

Related Questions