Run background process from .bashrc

I understand that this can be a stupid problem, but I can’t solve it for hours, even if you cleanly drew stackoverflow and google.

Here is the basic code in .bashrc to run gkrellm as soon as I enter the shell

if ps ax | grep -v grep | grep gkrellm > /dev/null then echo "gkrellm is already running" else command gkrellm & fi 

I already tried to try

 ... else nohup gkrellm & fi ... 

and

 ... else gkrellm $GK_PID=`pidof gkrellm` disown -h $GK_PID fi ... 

gkrellm fits correctly as a background job, and I can use the shell (as expected). BUT I still have gkrellm exiting as soon as I press Ctrl-c, even if I run other applications from the same shell. How to prevent gkrellm from closing when pressing Ctrl-c?

Just in case. I use a PuTTY clone called KiTTY, but I think this is not a problem.

Thanks for the help!

+4
source share
3 answers

using bash ( disown and &>/dev/null )
you need to run the application in bg ( gkrellm & ) and then disown it

 if ps -C gkrellm -o user | grep "$LOGNAME" &>/dev/null then echo "gkrellm is already running" else gkrellm & disown fi 

if you want to be more portable and use posix sh
you will need to use nohup (part of coreutils and POSIX)
as well as background ( nohup cmd & )
you would also use .profile instead of .bashrc

 if ps -C gkrellm -o user | grep "$LOGNAME" 2>/dev/null 1>&2 then echo "gkrellm is already running" else nohup gkrellm & fi 

other approaches will include, as @Pontus replied, using tools like dtach , screen or tmux , where the command is executed in a disconnected environment.

by Pontus:
it would be wiser to use the autorun functions of the window manager.

really :), since afaik gkrellm is a graphical application, it is better to autostart it using .xinitrc (if your registration manager supports it) or your autostart window manager.

+2
source

Try replacing "nohup gkrellm &" with this:

 screen -S gkrellm -d -U -m gkrellm 

This will start a remote session with gkrellm running, and it will not care about the current shell session. I'm not sure if starting it with .bashrc is the best solution, it would be wiser to use the autorun features of the window manager.

Edit: Not sure if I read the question correctly, are you using KiTTY to connect to the linux host and remotely launch gkrell via X forwarding? If so, you obviously cannot use the window manager functions. :)

+1
source

I almost forgot about this problem and answered my own question after I found a working solution for a long time;) Follow the work perfectly in my .bashrc for many years

 mygkrellm() { if pidof -x "gkrellm" >/dev/null; then echo "Gkrellm is already running. Go to shell!" else nohup "/usr/bin/gkrellm" & fi } 
+1
source

All Articles