Running a command as a background process/service

孤者浪人 提交于 2019-12-03 11:07:56
Sorpigal

Take a look at the daemon command, which can turn arbitrary processes into daemons. This will allow your script to act as a daemon without requiring you to do a lot of extra work. The next step is to invoke it automatically at boot. To know the correct way to do that, you'll need to provide your OS (or, for Linux, your distribution).

UNIX systems can handle as many processes as you need simultaneously (just open new shell windows if you're in a GUI), so running a process in the background is only necessary if you need to carry on using the current shell window for other things once you've run an application or process that keeps running.

To run a command called command in background mode, you'd use:

command &

This is a special character that returns you to the command prompt once the process is started. There are other special characters that do other things, more info is available here.

Based on this article:
http://felixmilea.com/2014/12/running-bash-commands-background-properly/

...another good way is with screen eg:

screen -d -m -s "my session name" <command to run>

from the screen manual:

-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

i.e. you can close your terminal, the process will continue running (unlike with &)

with screen you can also reattach to the session later

For advanced job control with bash, you should look into the commands jobs bg and fg.

However, it seems like you're not really interested in running the command in the background. What you want to do is launch the command at startup. The way to do this varies depending on the Unix system you use, but try to look into the rc family of files (/etc/rc.local for example on Ubuntu). They contain scripts that will be executed after the init script.

Use nohup while directing the output to /dev/null

nohup command &>/dev/null &

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!