site  contact  subhomenews

Interesting way to change name of current process

October 04, 2021 — BarryK

Well, interesting to Linux shell script programmers anyway.

I'm running chromium as a non-root user 'chromium'. /usr/bin/chromium is a script that performs login to user 'chromium' then runs the real chromium executable.

I had the situation where clicked on the close-box top-right of the chromium window, it closed, but was unable to restart chromium. Found that some chromium processes were still running. These have to be killed. This also happens sometimes with seamonkey, leaving the user wondering why it won't start.

What I want to do in the /usr/bin/chromium script is kill all these leftover processes. Simply running "killall chromium" will also kill the current script. Utility 'ps' can be run to find these processes, then run 'kill' on each one. That's one way of doing it.

However, I found another way of doing it. This is the code inserted into /usr/bin/chromium:

#20211004
#sometimes seamonkey and chromium gui closes, but still processes running, which
#prevents restart. kill old $APPname processes this way...
#this will work if runs as non-root on main desktop, crippled-root in container.
if [ $Cflg -eq 1 ]; then
echo -n "new${APPname}" > /proc/${$}/comm #change name of current process.
killall ${APPname}
else
killall --user ${APPname} ${APPname} #requires full killall from psmisc pkg.
fi

...where $APPname='chromium' and $Cflg=1 when running in a container. The name change occurs in the first highlighted line.

Now, "killall chromium" will not kill the current process.

Or, if running on the main desktop, not in a container, the full 'killall' utility allows specification of a user. Again, this will not kill the current process. See second highlighted text.    

Tags: easy