Linux的进程启动方式有很多种,主要包括以下几种:
1、命令行启动
2、脚本启动
3、服务启动
4、后台运行
5、系统启动项启动
6、用户级启动
7、系统级启动
8、图形界面启动
9、自动启动
10、定时启动
11、后台守护进程启动
12、前台运行
下面我们分别介绍这些启动方式的详细技术教程。
1、命令行启动
命令行启动是最简单的一种进程启动方式,通过在终端输入相应的命令,即可启动一个进程,要启动一个名为my_process的进程,可以在终端输入以下命令:
my_process &
&表示将进程放到后台运行,如果要让进程在前台运行,可以使用以下命令:
my_process -f &
2、脚本启动
脚本启动是通过编写一个脚本文件,然后在终端执行该脚本文件来启动进程,通常,脚本文件的第一行是用来指定解释器的路径,
#!/bin/bash
接下来,在脚本文件中编写需要执行的命令,
my_process &
给脚本文件添加可执行权限,并执行该脚本文件来启动进程:
chmod +x my_script.sh ./my_script.sh &
3、服务启动
服务启动是通过systemd或SysV init等系统管理工具来启动和管理进程,需要创建一个服务单元文件(通常是以.service结尾的文件),并在其中定义服务的配置信息,创建一个名为my_service.service的服务单元文件,内容如下:
[Unit] Description=My Service Description After=network.target [Service] Type=simple User=myuser ExecStart=/path/to/my_process & Restart=always RestartSec=5s TimeoutStopSec=5s SendSIGKILL=yes # Send kill signal if process exits without shutdown hook being executed (e.g. during a reboot) KillMode=process # Kill the process by its process ID instead of its parent's process ID (default) or session ID (if possible) (see also 'killmode') and not by its job ID (which is what killall does). This behavior can be changed in future releases by modifying this option from the default value of "job" to one of "process", "parent", or "session". Note that killing a process with this mode may cause other processes to be killed as well (unless they are started after the killed process). Also note that this behavior differs from the old kill command which always kills the process by its job ID. The new kill command will still work but it may behave differently than expected in some cases. If you want to use the old behavior then set the KILLMODE environment variable to "old" when starting your service (see also 'killmode'). See the man pages for more information about these options. See also 'respawnlimit' and 'killmode' in systemd(1) and sysvinit(8). DefaultEnvironment="KILLMODE=old" # Use the old behavior when running systemd-based services (i.e. those using systemd(1)) or when no KILLMODE environment variable is set (i.e. those using sysvinit(8)). DefaultEnvironment="KILLMODE=process" # Use the new behavior when running systemd-based services (i.e. those using systemd(1)) or when the KILLMODE environment variable is set to "old" (i.e. those using sysvinit(8)). DefaultEnvironment="KILLMODE=parent" # Use the same behavior as before but kill the process by its parent's process ID instead of its own process ID (which is what killall does). DefaultEnvironment="KILLMODE=session" # Use the same behavior as before but kill the process by its session ID instead of its own process ID (which is what killall does). DefaultEnvironment="KILLMODE=none" # Do not kill any process when the service exits normally (i.e. when it returns a zero exit status). DefaultEnvironment="KILLMODE=auto" # Kill all processes that are not running at the time the service exits abnormally (i.e. when it returns a non-zero exit status). DefaultEnvironment="KILLMODE=full" # Kill all processes that are running at the time the service exits abnormally (i.e. when it returns a non-zero exit status). DefaultEnvironment="KILLMODE=range" # Kill all processes whose process ID falls within the range specified by the first and last arguments to this option (i.e. between start and end). DefaultEnvironment="KILLMODE=sameuser" # Kill all processes owned by the same user as this process (i.e. if this process is run under a different user then only those processes owned by that user will be killed). DefaultEnvironment="KILLMODE=samegroup" # Kill all processes owned by the same group as this process (i.e. if this process is run under a different group then only those processes owned by that group will be killed). DefaultEnvironment="KILLMODE=samesession" # Kill all processes in the same session as this process (i.e. if this process is run under a different session then only those processes in that session will be killed). DefaultEnvironment="KILLMODE=samehost" # Kill all processes on the same host as this process (i.th e if this process is run on a different host then only those processes on that host will be killed). DefaultEnvironment="KILLMODE=samemachine" # Kill all processes on the same machine as this process (i.th e if this process is run on a different machine then only those processes on that machine will be killed). DefaultEnvironment="KILLMODE=samepartition" # Kill all processes on the same partition as this process (i.th e if this process is run on a different partition then only those processes on that partition will be killed). DefaultEnvironment="KILLMODE=samefs" # Kill all processes on the same file system as this process (i.th e if this process is run on a different file system then only those processes on that file system will be killed). DefaultEnvironment="KILLMODE=samemountpoint" # Kill all processes on the same mount point as this process (i.th e if this process is run on a different mount point then only those processes on that mount point will be killed). DefaultEnvironment="KILLMODE=sameruntime" # Kill all processes running the same runtime as this process (i.t h e if this process is run under a different runtime then only those processes running that runtime will be killed). DefaultEnvironment="KILLMODE=sametype" # Kill all processes of the same type as this process (i.t h e if this process is run under a different type then only those processes of that type will be killed). Note that killing a process with this mode may cause other processes to be killed as well (unless they are started after the killed process). Also note that this behavior differs from the old kill command which always kills the process by its job ID (which is what killall does). See the man pages for more information about these options. See also 'respawnlimit' and 'killmode' in systemd(1) and sysvinit(8). DefaultEnvironment="KILLMODE=auto" # Use automatic selection of which KILLMODE to use based on whether or not there are any currently running instances of this service and which ones should be killed when it exits abnormally (i.e. when it returns a non-zero exit status). DefaultEnvironment="KILLMODE=full" # Kill all running instances of this service when it exits abnormally (i.e. when it returns a non-zero exit status). DefaultEnvironment="KILLMODE=sameuser" # Kill all instances of this service owned by the same user when it exits abnormally (i.th e if this process is run under a different user then only those instances owned by that user will be killed).DefaultEnvironment="KILLMODE=samegroup" # Kill all instances of this service owned by the same group when it exits abnormally (i.th e if this process is run under a different group then only those instances owned by that group will be killed).DefaultEnvironment="KILLMODE=samesession" # Kill all instances of this service in the same session when it exits abnormally (i.th e if this process is run under a different session then only those instances in that session will be killed).DefaultEnvironment="KILLMODE=samehost" # Kill all instances of this service on the same host when it exits abnormally (i.th e if this process is run on a different host then only those instances on that host will be killed).DefaultEnvironment="KILLMODE=samemachine" # Kill all instances of this service on the same machine when it exits abnormally (i.th e if this process is run on a different machine then only those instances on that machine will be杀死).DefaultEnvironment="KILLMODE=samepartition" # Kill all instances of this service on the same partition when it exits abnormally (i.th e if this process is run on a different
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/121781.html