Start background process from .bashrc

后端 未结 3 1000
遥遥无期
遥遥无期 2021-01-03 06:35

I realize that this might be a stupid issue but I can\'t solve it for hours even if digged stackoverflow site and google throughly.

Here is the base code in .bashrc

3条回答
  •  鱼传尺愫
    2021-01-03 06:59

    using bash (disown and &>/dev/null)
    you need to run the app in the 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)
    and also background it (nohup cmd &)
    you'd 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 would include - as @Pontus answered - using tools like dtach, screen or tmux, where the command is executed in a detached environment.

    by Pontus:
    it would make more sense to use your window manager's autostart features

    indeed :) as afaik gkrellm is a GUI app, it's better to autostart it, either using .xinitrc (if your login manager supports it), or your window manager's autostart facilities.

提交回复
热议问题