server daemon

code run 

There are several ways to get code to run in the background on the server. In Linux systems, the most common methods are as follows:

**1. Use the `&` symbol:**

Adding the `&` symbol after the command will make the program run in the background. For example:

```bash
python myscript.py &
```

But there is a problem with this approach: when you exit the shell, the program will be killed.

**2. Use the `nohup` command:**

The `nohup` command allows programs to be run without a terminal. That way, even if you exit the shell, the program won't be killed.

```bash
nohup python myscript.py > output.log &
```

Here, `> output.log` is to redirect the output of the program to the `output.log` file, and the `&` symbol is to put the program in the background.

**3. Using `screen` or `tmux`:**

`screen` and `tmux` are tools under Linux that allow you to create multiple shell windows (sessions) and run programs in them. These windows (sessions) continue to run even after you exit the shell.

For example, using `screen` you can do this:

```bash
screen -S mysession  # 创建一个新的 screen 会话
python myscript.py   # 在这个会话中运行程序
Ctrl+A, D            # 按下 Ctrl+A,然后按下 D,以便脱离当前 screen 会话,但程序会继续运行
```

You can then reconnect to this screen session at any time to see the program in action:

```bash
screen -r mysession
```

**4. Use system services (such as Systemd or SysV init):**

This is a more complex approach and is usually used for long-running programs, such as server programs. You need to write a service script, specify how to start and stop your program, and then register as a system service. You can then use the system's service management commands such as `systemctl start myservice` to control your program.

view log

When using the second method (`nohup` command), the operation of the program is usually carried out by looking at the output file you specify. For example, if the command you use is:

```bash
nohup python myscript.py > output.log &
```

Then you can see the operation of the program by looking at the `output.log` file:

```bash
cat output.log
```

The `cat` command is used to display the contents of a file.

Another way to view the contents of the `output.log` file is to use the `tail` command, which displays the last few lines of the file. For example, the `tail -f output.log` command will continue to display the latest contents of the `output.log` file until you stop it.

```bash
tail -f output.log
```

The `tail -f` command is often used to view the log file being updated.

In this way, during the running of the program, the output of the program can be viewed in real time to understand the running status of the program.

 view progress

If you want to view running background processes, you can use psthe command. For example, ps aux | grep pythonyou can list all pythonprocesses containing the keyword. ps auxThe meanings of the columns displayed by the command are as follows:

  • USER: which user is running the process
  • PID: ID of the process
  • %CPU: The CPU usage rate occupied by the process
  • %MEM: The percentage of physical memory and total memory occupied by the process
  • VSZ: Amount of virtual memory (KB) used by the process
  • RSS: The fixed amount of memory (KB) occupied by the process (the number of resident pages)
  • STAT: the status of the process
  • START: The process is triggered to start time
  • TIME: The actual CPU time of the process
  • COMMAND: the name and parameters of the command

 kill process

 

In Linux systems, you can use the `kill` command to kill (end) a process. This command requires you to know the process ID (PID) of the process you want to end. You can find this PID by using the `ps` command.

For example, suppose you want to kill a Python program called `myscript.py`, you can first use the `ps` command to find its PID:

```bash
ps aux | grep myscript.py
```

This command will list all processes containing "myscript.py". PID is the second column in the result.

Then, you can use the `kill` command to end the process:

```bash
kill -9 <PID>
```

In this command, `<PID>` is the PID of the process you want to kill, and `-9` is a signal that means "force kill the process". If `-9` is not added, the system will first try to end the process in a more gentle way.

It should be noted that only the owner of the process or the root user can kill a process.

You can also use the `pkill` or `killall` commands to kill one or more processes. For example, `pkill myscript.py` will kill all processes named "myscript.py". However, these two commands are not supported by all systems.

Guess you like

Origin blog.csdn.net/qq_25368751/article/details/131934205