Ejecutar scripts de Linux o Ubuntu en segundo plano

Tiempo de lectura: 3 minutos

Reading time: 2 minutes

Today I’m going to show you how to run scripts even after closing the Bash console, in other words, in the background.

Running Scripts in the Background

To execute a script in the background, you can use the & operator. The & operator at the end of a command tells the shell to run it in the background and continue accepting input.

To run a script in the background, follow these steps:

  1. Open a Ubuntu terminal.
  2. Navigate to the folder where the script you want to execute is located. You can use the cd command to move to the folder.bash
cd /path/to/folder

Execute the script using the & operator at the end of the command.

  1. ./script.sh & Where ./script.sh is the name of the script you want to execute.
  2. The script will run in the background, and the terminal will be available to receive new input.

Using nohup

nohup is a tool that allows you to run commands in the background even when the terminal is closed. It is especially useful for running long-running processes or background scripts that need to be protected from accidental interruptions.

To install nohup on Ubuntu, follow these steps:

  1. Open a Ubuntu terminal.
  2. Run the following command to install nohup:arduino
sudo apt-get install nohup

Enter your Ubuntu user password if prompted.

Once the installation is complete, you can now use nohup to run commands in the background. The following is an example of how to use nohup to run a script in the background:

nohup ./script.sh &

Where ./script.sh is the name of the script you want to execute.

The script will run in the background and will be logged to a file named nohup.out. This file will be created in the current directory from which the command was executed.

Note: If you want to change the name of the output file, you can use the following command:

  1. nohup ./script.sh > output_file.txt & Where output_file.txt is the name of the output file you want to use.

With these steps, you can now run scripts in the background and protect them from accidental interruptions using nohup.

Reading time: 2 minutes

Today I’m going to show you how to run scripts even after closing the Bash console, in other words, in the background.

Running Scripts in the Background

To execute a script in the background, you can use the & operator. The & operator at the end of a command tells the shell to run it in the background and continue accepting input.

To run a script in the background, follow these steps:

  1. Open a Ubuntu terminal.
  2. Navigate to the folder where the script you want to execute is located. You can use the cd command to move to the folder.bash
cd /path/to/folder

Execute the script using the & operator at the end of the command.

  1. ./script.sh & Where ./script.sh is the name of the script you want to execute.
  2. The script will run in the background, and the terminal will be available to receive new input.

Using nohup

nohup is a tool that allows you to run commands in the background even when the terminal is closed. It is especially useful for running long-running processes or background scripts that need to be protected from accidental interruptions.

To install nohup on Ubuntu, follow these steps:

  1. Open a Ubuntu terminal.
  2. Run the following command to install nohup:arduino
sudo apt-get install nohup

Enter your Ubuntu user password if prompted.

Once the installation is complete, you can now use nohup to run commands in the background. The following is an example of how to use nohup to run a script in the background:

nohup ./script.sh &

Where ./script.sh is the name of the script you want to execute.

The script will run in the background and will be logged to a file named nohup.out. This file will be created in the current directory from which the command was executed.

Note: If you want to change the name of the output file, you can use the following command:

  1. nohup ./script.sh > output_file.txt & Where output_file.txt is the name of the output file you want to use.

With these steps, you can now run scripts in the background and protect them from accidental interruptions using nohup.

Leave a Comment