It has been half a month since the previous article. I feel sorry for this time. Because now I am learning Java, and I will have time to talk to you later.
It has been half a month since the previous article. I feel sorry for this time. Because now I am learning Java, and I will have time to share my learning experience with you later.
Today, I want to share my knowledge about how to establish a process. In Linux, how should we proceed? let's take a good look.
Example 1:
In this example, a background process is created.
[Root @ instructor Desktop] # ping 127.0.0.1 &
[1] 16885
[Root @ instructor Desktop] # PING 127.0.0.1 (127.0.0.1) 56 (84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq = 1 ttl = 64 time = 0.353 MS
64 bytes from 127.0.0.1: icmp_seq = 2 ttl = 64 time = 1.05 MS
64 bytes from 127.0.0.1: icmp_seq = 3 ttl = 64 time = 0.065 MS
64 bytes from 127.0.0.1: icmp_seq = 4 ttl = 64 time = 0.069 MS
64 bytes from 127.0.0.1: icmp_seq = 5 ttl = 64 time = 0.066 MS
64 bytes from 127.0.0.1: icmp_seq = 6 ttl = 64 time = 0.146 MS
Fg
Ping 127.0.0.1
64 bytes from 127.0.0.1: icmp_seq = 7 ttl = 64 time = 0.067 MS
64 bytes from 127.0.0.1: icmp_seq = 8 ttl = 64 time = 0.070 MS
64 bytes from 127.0.0.1: icmp_seq = 9 ttl = 64 time = 0.069 MS
64 bytes from 127.0.0.1: icmp_seq = 10 ttl = 64 time = 0.072 MS
^ C
--- 127.0.0.1 ping statistics ---
10 packets transmitted, 10 bytes ed, 0% packet loss, time 9471 ms
Rtt min/avg/max/mdev = 0.065/0.202/1.050/0.295 MS
[Root @ instructor Desktop] #
In fact, there are two methods to establish processes in Linux: one is to run the program on the foreground, and the other is to run the program on the background.
1. running the program on the front-end means the normal execution of the command without adding any symbols or names.
Example 2:
[Root @ instructor Desktop] # ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56 (84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq = 1 ttl = 64 time = 0.102 MS
64 bytes from 127.0.0.1: icmp_seq = 2 ttl = 64 time = 0.070 MS
64 bytes from 127.0.0.1: icmp_seq = 3 ttl = 64 time = 0.071 MS
64 bytes from 127.0.0.1: icmp_seq = 4 ttl = 64 time = 0.082 MS
^ C
--- 127.0.0.1 ping statistics ---
4 packets transmitted, 4 bytes ed, 0% packet loss, time 3401 ms
Rtt min/avg/max/mdev = 0.070/0.081/0.102/0.014 MS
[Root @ instructor Desktop] #
2. Background Processes
At the beginning of the article, we gave the first example. we learned that by adding the "&" symbol at the end of the running command, the command can be run in the background.
Example 3:
[Root @ instructor Desktop] # ping 192.168.0.254 &
[1] 17077
[Root @ instructor Desktop] # PING 192.168.0.254 (192.168.0.254) 56 (84) bytes of data.
64 bytes from 192.168.0.254: icmp_seq = 1 ttl = 64 time = 0.205 MS
64 bytes from 192.168.0.254: icmp_seq = 2 ttl = 64 time = 0.112 MS
64 bytes from 192.168.0.254: icmp_seq = 3 ttl = 64 time = 0.100 MS
64 bytes from 192.168.0.254: icmp_seq = 4 ttl = 64 time = 0.065 MS
^ C
[Root @ instructor Desktop] #64 bytes from 192.168.0.254: icmp_seq = 5 ttl = 64 time = 0.107 MS
64 bytes from 192.168.0.254: icmp_seq = 6 ttl = 64 time = 0.131 MS
64 bytes from 192.168.0.254: icmp_seq = 7 ttl = 64 time = 0.110 MS
64 bytes from 192.168.0.254: icmp_seq = 8 ttl = 64 time = 0.980 MS
64 bytes from 192.168.0.254: icmp_seq = 9 ttl = 64 time = 0.111 MS
Fg
Ping 192.168.0.254
64 bytes from 192.168.0.254: icmp_seq = 10 ttl = 64 time = 0.100 MS
64 bytes from 192.168.0.254: icmp_seq = 11 ttl = 64 time = 0.110 MS
64 bytes from 192.168.0.254: icmp_seq = 12 ttl = 64 time = 0.070 MS
^ C
--- 192.168.0.254 ping statistics ---
12 packets transmitted, 12 bytes ed, 0% packet loss, time 11461 ms
Rtt min/avg/max/mdev = 0.065/0.183/0.980/0.242 MS
[Root @ instructor Desktop] #
3. commands and shortcuts for background process control
1. Ctrl-Z
Ctrl-Z can place a command being executed on the foreground to the background and pause
2. bg
Bg changes a command that is paused in the background to continue execution.
If there are multiple commands in the background, you can use bg % jobnumber to call up the selected command. % jobnumber is the serial number (not pid) of the command being executed in the background found by the jobs command)
3. fg
If you want to call it back to the foreground, you can use
4. jobs
View the number of commands currently running in the background
Example 4:
Suppose you find it takes a long time to run a program on the front end, but you need to do other things, you can use Ctrl-Z to suspend the program, then you can see the system prompt (the job number in square brackets ):
[1] + Stopped/root/bin/rsync. sh
Then we can schedule the program to the background for execution: (the number following bg is the job number)
# Bg 1
[1] +/root/bin/rsync. sh &
Run the jobs command to view running tasks:
# Jobs
[1] + Running/root/bin/rsync. sh &
If you want to call it back to the foreground, you can use
# Fg 1
/Root/bin/rsync. sh
In this way, you can only wait for the task to be completed on the console.
Fg, bg, jobs, &, ctrl + z are all related to system tasks. although these commands are rarely used, they are also very practical.