Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm pretty sure you cant do this but maybe someone has some weird hack...

Is there a way to get comments (#blahblah) to show up in ps output?

My problem is that i have a script, lets call it "myscript.sh" that is not very descriptive.

I'd love to be able to do something like:

 ./myscript.sh #Running on 10.10.45.10

and have ps output the comment line, instead of just saying "myscript.sh"

The only way i can think of hacking around it would be to place the comment in a variable, grep out "myscript.sh" PID and then read that var from /proc/PID/environ. But thats very ugly, and requires root

share
2  
Does myscript.sh accept command line arguments? If not, /myscript.sh Running on 10.10.45.10 should work. If yes, can you edit it to handle a special argument too? If yes, /myscript.sh -c 'Running on 10.10.45.10' should work, just instruct it to not fail when seeing -c arguments. –  manatwork Dec 27 '12 at 15:21
1  
You don't need to be running as root to access your own processes. ps (at least the one from the procps package) has also option the e which displays the environment right away, you can also parse output of that (unfortunately the environment is not separated from the command name, which is lame). –  peterph Dec 27 '12 at 17:09

1 Answer 1

up vote 0 down vote accepted

With zsh, you could do something like:

#! /bin/zsh -
(($+ARG0_SET)) || ARG0_SET= ARGV0="#Running on 10.10.45.10" exec zsh "$0" "$@"
ps -f -p "$$"

Which when run gives:

~$ ./a
UID        PID  PPID  C STIME TTY          TIME CMD
chazelas 20157  8822  0 21:51 pts/1    00:00:00 #Running on 10.10.45.10 ./a

The ksh93 equivalent would be:

#! /bin/ksh93 -
((${#ARG0_SET})) || ARG0_SET=yes exec -a "#Running on 10.10.45.10" ksh93 "$0" "$@"
ps -f -p "$$"
share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .