linux practical background task

linux practical background task

linux using a background task

&          # 这个用在一个命令的最后,可以把这个命令放到后台执行
ctrl + z   #可以将一个正在前台执行的命令放到后台,并且暂停
jobs       #查看当前有多少后台运行的命令
fg         #把后台命令调到前台继续运行 | 如果后台有多个任务 fg后面接序号(jobs命令查看任务序号)
bg         # 将后台暂停的命令,变成继续执行, 多个任务后面接序号 )
ctrl+c      # 前台进程的终止

nohup   #不挂断地运行命令,退出用户后还继续运行的
     nohup command > myout.file 2>&1 &   //输出被重定向到myout.file文件中。
   nohup<程序名>&    //则控制台logout后,进程仍然继续运行,起到守护进程的作用(虽然它不是严格意义上的守护进程)。
无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。
如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。
nohup startWeblogic.sh&
如果想要监控标准输出可以使用:
tail -f nohup.out


   

# Use under Linux Shell command control tasks Jobs execute
the following command can be used to manipulate the process task:
  PS list of processes running on the system;
  the kill to send signals to one or more processes (often used to kill a process);
  Jobs column the state of the current task shell environment has been started, if not specified jobsid, task status information is displayed for all activities; if reported terminate a task (that is, the task of the state is labeled terminated), shell has been from the current shell environment delete a task list known process ID;
  BG will move the process running in the background (background);
  fg process will move to the foreground (foreground); 

1, run .sh file

It can directly run by ./sh file, but if you want to run in the background, even if you close the current terminal can also run it, and the need nohup command & command.

(1) & command
         functions: added a command Finally, you can put this command in the background

(2) nohup command
         function: do not hang up the run command
nohub ./red5.sh &
nohup ./test &

2, view the current command running in the background

There are two commands you can use, jobs and ps, the difference between the current terminal jobs for viewing tasks running in the background, for a terminal to see it. The ps command is used to view the dynamic process of the moment, you can see the other end background processes running.

(1) jobs command
       functions: to view the current terminal tasks running in the background
[root @ CON ~] # Jobs
[1] + Stopped /root/a.txt vim
[2] vim aa.txg stopped
[3] - Stopped vim bbb.txt  

      jobs -l option to display all tasks of the current terminal PID, jobs status can be running, stopped, Terminated. + Sign indicates that the current task - a task after the number indicates.

(2) ps command

         Features: View all the current process
[root @ CON ~] # PS
 PID TTY the TIME CMD
6367 PTS / bash 2 00:00:00
6880 PTS / vim 2 00:00:00
6960 PTS / vim 2 00:00:00
7141 pts / 2 00:00:00 ps

  ps -aux | grep "test.sh" #a: displays all programs u: user-based format to display x: displays all programs, not to the terminal to distinguish

3, close the command is currently running in the background

     kill command: end of the process

    (1) View jobnum by the jobs command, and then execute kill% jobnum

    (2) to view the process ID PID with the ps command and then kill% PID

      If the foreground process is then performed directly Ctrl + c can be terminated

4, switching and control of front and back office processes

    (1) fg command

      Function: The command in the background adjusted to the front desk to continue running

      If more than one command in the background, you can see jobnun with jobs, then called up using the fg% jobnum the selected command.

    (2) Ctrl + z command

      Function: a command being executed in the foreground into the background, and in a suspended state

    (3) bg command

      Function: a pause command in the background, becomes continue in the background

      If more than one command in the background, you can see jobnum with jobs, then bg% jobnum the selected command recall to proceed.

screen use

What 1 screen command?

Screen is a physical terminal may be multiplexed among a plurality of processes a full-screen window manager. Screen in the concept of a session, users can create multiple screen windows in a screen session, each operating as a screen window in a real telnet / SSH connection window so.

2 How to Install screen command

centos   yum install screen
debian/ubuntu    apt-get install screen

3 screen commands to use?

screen -S lnmp  #将建名字为lnmp的会话
Ctrl + a d (按住ctrl 以此再按a,d)  # 临时离开当前会话
screen -r lnmp #恢复lnmp会话工作界面 如果忘记会话名 screen -ls 
screen -r 11792  screen 回话进程id
exit  会提示 screen is terminating 表示成功退出screen会话。

远程演示
screen -S test 创建一个screen会话    另一个终端界面通过 screen -x test  查看

常用快捷键
Ctrl+a c :在当前screen会话中创建窗口
Ctrl+a w :窗口列表
Ctrl+a n :下一个窗口
Ctrl+a p :上一个窗口
Ctrl+a 0-9 :在第0个窗口和第9个窗口之间切换

Guess you like

Origin www.cnblogs.com/chengkanghua/p/11856320.html