Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the process name of a shell script?

Is there any way to set the process name of a shell script? This is needed for killing this script with the killall command.

like image 944
dan Avatar asked Jun 19 '10 13:06

dan


People also ask

How do I get the process name from PID?

In this quick article, we've explored how to get the name and the command line of a given PID in the Linux command line. The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

What is process shell script?

A process is program (or command typed by user) to perform specific Job. In Linux when you start a process, it is given a unique number called a PID or process-id. PIDs start from 0 to 65535. PID 1 is always assigned to init process, which is the first process started at boot time.

WHAT IS SET command in shell?

The set command is a built-in Linux shell command that displays and sets the names and values of shell and Linux environment variables. On Unix-like operating systems, the set command functions within the Bourne shell ( sh ), C shell ( csh ), and Korn shell ( ksh ).


6 Answers

Here's a way to do it, it is a hack/workaround but it works pretty good. Feel free to tweak it to your needs, it certainly needs some checks on the symbolic link creation or using a tmp folder to avoid possible race conditions (if they are problematic in your case).

Demonstration

wrapper

#!/bin/bash
script="./dummy"
newname="./killme"

rm -iv "$newname"

ln -s "$script" "$newname"

exec "$newname" "$@"

dummy

#!/bin/bash
echo "I am $0"
echo "my params: $@"

ps aux | grep bash

echo "sleeping 10s... Kill me!"
sleep 10

Test it using:

chmod +x dummy wrapper
./wrapper some params

In another terminal, kill it using:

killall killme

Notes

Make sure you can write in your current folder (current working directory).

If your current command is:

/path/to/file -q --params somefile1 somefile2

Set the script variable in wrapper to /path/to/file (instead of ./dummy) and call wrapper like this:

./wrapper -q --params somefile1 somefile2
like image 87
Weboide Avatar answered Oct 04 '22 05:10

Weboide


You can use the kill command on a PID so what you can do is run something in the background, get its ID and kill it

PID of last job run in background can be obtained using $!.

echo test & echo $!

like image 32
Abs Avatar answered Oct 04 '22 04:10

Abs


You cannot do this reliably and portably, as far as I know. On some flavors of Unix, changing what's in argv[0] will do the job. I don't believe there's a way to do that in most shells, though.

Here are some references on the topic.

  • Howto change a UNIX process and child process name by modifying argv0
  • Is there a way to change the effective process name in Python?
like image 41
Brian Clapper Avatar answered Oct 04 '22 04:10

Brian Clapper


This is an extremely old post. Pretty sure the original poster got his/her answer long ago. But for newcomers, thought I'd explain my own experience (after playing with bash for a half hour). If you start a script by script name w/ something like:

./script.sh

the process name listed by ps will be "bash" (on my system). However if you start a script by calling bash directly:

/bin/bash script.sh /bin/sh script.sh bash script.sh

you will end up with a process name that contains the name of the script. e.g.:

/bin/bash script.sh

results in a process name of the same name. This can be used to mark pids with a specific script name. And, this can be useful to (for example) use the kill command to stop all processes (by pid) that have a process name containing said script name.

like image 41
display_name_11011 Avatar answered Oct 04 '22 05:10

display_name_11011


You can all use the -f flag to pgrep/pkill which will search the entire command line rather than just the process name. E.g.

./script &
pkill -f script
like image 21
sheridp Avatar answered Oct 04 '22 05:10

sheridp


Include #![path to shell]

Example for path to shell -

  1. /usr/bin/bash
  2. /bin/bash
  3. /bin/sh

Full example

#!/usr/bin/bash
like image 29
Executer Avatar answered Oct 04 '22 04:10

Executer