Bash Script, Kill process by pulling from PID file Bash Script, Kill process by pulling from PID file linux linux

Bash Script, Kill process by pulling from PID file


Edit the script that starts glassfish and place something like echo $$ > /path/to/PID-file (this can contain ~ for home directory or some other mechanism like $USER to make user specific) on the line immediately following the line starting the process. You can then kill the correct process using kill $(cat /path/to/PID-file).


ps aux | grep ^$USER | grep glassfish | grep domain1 | gawk '{print $2}' | xargs kill -9

Below i did mistake with ps switches, so above grep should be fine.


ah it is not working, ps could be use like this ps -ao pid,tty,comm -u $USER, this grep above should be fine ...

someone else is logged in ...

If so, add switch -u

ps aux -u $USER | grep glassfish | grep domain1 | gawk '{print $2}' | xargs kill -9

$USER is user name that will be selected and listed, by default should be already set in OS environment. Multiple users could be selected by comma ps aux -u root,$USER

Take a note: If there is no specific username in the system, ps will throw ERROR: User name does not exist.

Read man ps for more.

-u userlist Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist. The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to U and --user.