Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Keep track of stopped processes via shell variables System

Earlier hints discuss how to suspend and resume processes via kill -- very helpful to me, because I had been launching apps from Terminal and using ^Z, fg and bg to manage them.

A summary of previous hints:

kill -STOP and kill -CONT allow you to pause and resume CPU-hogging processes. Use the Activity Monitor to discover the Process IDs or ps -ax and grep for the program name.
The problem comes when you need to juggle several programs at once. Recently, I've been using MacBochs with FreeDos to run Agenda, while using Microsoft Word and Adobe Acrobat -- all of which hog the CPU. It finally occured to me to set some shell variables. e.g.:
 % set WORD=1002
 % set AGENDA=4787
 % set ACROBAT=603
I used these to issue the kill commands. e.g.:
 % kill -STOP $ACROBAT
 % kill -CONT $AGENDA
Now, when I scroll through my command-line history, I can immediately identify which commands control which programs. It's not rocket science. It's not miraculous. But it is awfully convenient.
    •    
  • Currently 3.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[6,753 views]  

Keep track of stopped processes via shell variables | 6 comments | Create New Account
Click here to return to the 'Keep track of stopped processes via shell variables' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Keep track of stopped processes via shell variables
Authored by: colmc on Dec 15, '03 11:44:57AM
To make this more general you can use the following commands to find the process id of a program i.e. to find the Process Id of the Running Terminal.app you could use:

% ps -auxww | grep Terminal | grep -v grep | awk '{print $2}'
So to make your WORD variable up to date you could use, in csh:

% setenv WORD `ps -auxww | grep "Microsoft Word" | grep -v grep | awk '{print $2}'`
or in bash:

% export WORD=`ps -auxww | grep "Microsoft Word" | grep -v grep | awk '{print $2}'`
These variables can then be put in your .cshrc or .bashrc scripts so they're always available and always point to the correct process id.

[ Reply to This | # ]
Keep track of stopped processes via shell variables
Authored by: LC on Dec 15, '03 12:32:08PM
To shorten your shell commands, either use brackets e.g.
ps -ax | grep '[k]ext' | awk '{print $1}'
or, in sh/ksh/bash etc. it's easier to set and shift (positionals) ...
but there is --
killall [-d | -v] [-h | -?] [-help] [-l] [-m] [-s] [-u user] [-t tty] [-c procname] [-SIGNAL] [procname ...]
Also, if you started any commands from this shell (term), can just
jobs -l
then
kill %1
bg %2

etc. Larry

[ Reply to This | # ]
Keep track of stopped processes via shell variables
Authored by: LC on Dec 15, '03 12:39:15PM
Also, in a shell wrapper the pid of a launched (bg) command is available -- (csh)
some_command & ; set pid = ${!}
kill ${pid}


(sh)
pid=${!}

Larry.

[ Reply to This | # ]
Keep track of stopped processes via shell variables
Authored by: LC on Dec 15, '03 12:42:31PM
Oh yeah, to make those greps correct (in case there are multiple binaries running with similar names), need to --
ps -ax | grep '[k]ext' | head -1 | awk '{print $1}'
i.e. use head or tail to stuff just the first one into your variable; Larry.

[ Reply to This | # ]
Keep track of stopped processes via shell variables
Authored by: seann on Dec 15, '03 06:34:43PM

killall -STOP acrobat

..



[ Reply to This | # ]
killall
Authored by: gatorparrots on Dec 16, '03 04:05:32PM
As noted, killall is the tool for the job, as it takes named arguments versus PIDs with kill (which would require additional pipes to grep and awk processes). To better facilitate pausing/continuing processes, set up shell aliases like so:

Bourne shell style:
pause="killall -STOP $1"
cont="killall -CONT $1"

C shell style:
pause="killall -STOP \!:1"
cont="killall -CONT \!:1"


[ Reply to This | # ]