YouTip LogoYouTip

Linux Comm Nohup

## Linux nohup Command The **`nohup`** command (short for **no hang up**) is a Linux utility that allows you to run command-line processes in the background such that they do not get terminated even after you log out or exit the terminal. By default, when you close a terminal session, all running child processes receive a `SIGHUP` (Signal Hang Up) signal and terminate. The `nohup` command intercepts this signal, allowing your scripts or programs to run continuously in the background. --- ### Key Features * **Output Redirection:** By default, if you do not redirect the output of your command, `nohup` automatically redirects both standard output (`stdout`) and standard error (`stderr`) to a file named `nohup.out` in the current working directory. If the current directory is not writable, it redirects the output to `$HOME/nohup.out`. * **Permissions:** Available to all system users. --- ### Syntax and Usage ```bash nohup Command [Arg ...] [&] ``` #### Parameter Descriptions: * **`Command`**: The command, script, or program you want to execute. * **`Arg`**: Arguments or parameters passed to the command (e.g., input/output redirection paths). * **`&`**: The background execution operator. While `nohup` prevents the process from terminating on logout, appending `&` forces the command to run in the background immediately, freeing up your terminal prompt. --- ### Practical Examples #### 1. Running a Script in the Background To run a shell script named `runoob.sh` in the background and ensure it keeps running after you close the terminal: ```bash nohup /root/runoob.sh & ``` Upon executing this command, you will see a confirmation message in the terminal: ```text appending output to nohup.out ``` This indicates that the script is running successfully and its output is being written to `nohup.out`. --- #### 2. Terminating a `nohup` Process Since the process runs in the background, you cannot stop it using `Ctrl + C`. To stop it, you must find its Process ID (PID) and terminate it manually. **Step 1: Find the PID of the running process** You can use the `ps` command combined with `grep` to locate your process: ```bash ps -aux | grep "runoob.sh" ``` * **`a`**: Displays processes for all users. * **`u`**: Displays the process list in a user-oriented format (showing owner, CPU/Memory usage, etc.). * **`x`**: Displays processes not associated with a terminal. Alternatively, you can use the System V style `ps` command: ```bash ps -ef | grep "runoob.sh" ``` **Step 2: Kill the process** Once you identify the PID (e.g., `12345`), terminate it using the `kill` command: ```bash kill -9 12345 ``` *(Note: `-9` sends the `SIGKILL` signal, forcing the process to terminate immediately).* --- #### 3. Redirecting Output to a Custom Log File If you do not want your output saved to `nohup.out`, you can redirect both standard output (`stdout`) and standard error (`stderr`) to a custom log file: ```bash nohup /root/runoob.sh > runoob.log 2>&1 & ``` ##### Explanation of `2>&1`: In Linux, file descriptors are represented by integers: * **`0`** – `stdin` (Standard Input) * **`1`** – `stdout` (Standard Output) * **`2`** – `stderr` (Standard Error) * **`> runoob.log`**: Redirects standard output (`1`) to `runoob.log`. * **`2>&1`**: Redirects standard error (`2`) to the same file descriptor as standard output (`&1`). This ensures both normal logs and error logs are consolidated into `runoob.log`. * **`&`**: Runs the entire operation in the background. --- ### Summary of Best Practices | Use Case | Command | | :--- | :--- | | **Default Background Run** | `nohup command &` | | **Custom Log File** | `nohup command > output.log &` | | **Discard All Output (Silent)** | `nohup command > /dev/null 2>&1 &` | | **Find Running Nohup Jobs** | `ps -ef | grep command` |
← Js AsyncJava Iterator β†’