诺甘农圆盘

参考资料收集库

用户工具

站点工具


linux:bash:remote

远程执行文件替换重启脚本

有时需要更新文件,但是停止服务又会导致远程连接丢失,用脚本在远程ssh执行会更停止服务就断掉,后面的bash无法执行了,找了一下怎么挂在后台继续运行脚本,AI回答方法如下:

研究了下,基本使用办法2里面的nohup最简单,不用另外装东西。

To run a Bash command or script in the background, preventing it from blocking your terminal and allowing you to continue working, several methods are available:

1. Using the Ampersand (&) Operator:

Append an ampersand (&) to the end of your command or script execution. This will immediately place the process in the background.

Code

your_command_or_script &

For example:

Code

sleep 60 &

2. Using nohup:

The nohup command ensures that a process continues to run even if the terminal session is closed. It also redirects standard output and error to a file named nohup.out by default.

Code

nohup your_command_or_script &

To prevent output from being written to nohup.out, you can redirect standard output and error to /dev/null:

Code

nohup your_command_or_script > /dev/null 2>&1 &

3. Using Job Control (for already running processes):

If you have a process running in the foreground that you want to move to the background:

  • Pause the process:Press Ctrl+Z. This will suspend the process and return control to the terminal.
  • Move to background:Type bg and press Enter. This will resume the suspended process in the background.

To bring a backgrounded process back to the foreground, use fg.

4. Using Terminal Multiplexers (e.g., screen or tmux):

Terminal multiplexers allow you to create persistent sessions that can be detached and reattached, effectively running processes in the background even if you close your terminal window.

  • Start a new session:
    • screen -S session_name
    • tmux new -s session_name
  • Run your command/script within the session.
  • Detach the session:
    • Ctrl+A d (for screen)
    • Ctrl+B d (for tmux)
  • Reattach to the session:
    • screen -r session_name
    • tmux attach -t session_name
linux/bash/remote.txt · 最后更改: packingbox