Run a script on Ubuntu or Linux in the background (continues to run when you close the console)

Tiempo de lectura: < 1 minuto

Sometimes we want to keep a script running on our Ubuntu or Linux machine, but we don’t have a system service created. With this command, we can keep this script running in the background even if we close the console.

The first thing we need to do is install nohup (if it’s not already active on our system).

dpkg -S /usr/bin/nohup

Usually, it comes pre-installed, but if it’s not installed, we need to install it using this command.

The usage is simple, to execute it, we just need to type nohup followed by the command we want to run.

For example, if we want to run the Telegram bot from the tutorial Cómo hacer un bot para Telegram (Python), we just need to type the following:

nohup python3 bot.py

This command works for any command we want to invoke.

If we want to redirect the output of the script to a file, we need to invoke it as follows:

nohup script.sh > script.out 2> script.err

Remember to replace script.sh with the actual program or script you want to invoke.

Leave a Comment