Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How can I execute a program, but send it to the background?

Published

Linux and Commands

Ubuntu is a popular Linux distribution that is widely used by developers, system administrators, and enthusiasts alike. One of the most common tasks in Ubuntu is running programs from the command line. While running programs in the foreground is straightforward, running them in the background can be a bit more challenging.

Fortunately, Ubuntu provides several options to run programs in the background, including using the ampersand symbol, the nohup command, and the screen utility. In this tutorial, we will walk you through the steps to execute programs in Ubuntu and run them in the background using the built-in tools available in the operating system. By the end of this tutorial, you will be able to run programs in the background and continue to use your terminal for other tasks.

Execute Program/Command in Background

To execute any command in background, Add “&” at the end of the command. When you do this, You will get the process ID in return. You can use this process ID to control the execution of your program in background. For example, In the following command, we are executing a script called ftpupdate in background.

$ /scripts/ftpupdate &

This will run the ftpupdate command in the background and return control of the terminal to you. Note that if your command or program generates output, The output will still be visible on the terminal. To redirect output to a file, modify the command to add “> filename &” at the end of the command. Here is the example.

$ /scripts/ftpupdate &> /path/to/output/file.txt &

Now, this command will send the ftpupdate execution in the background and will write the output in the output file we defined in the command. It will no longer show output in the terminal itself.

Kill the Process

It’s important to manually kill the process if required. To do so, Note down the process ID generated when executing the command or a program in background. You will get the process ID as soon as you send the command to background. It should look like the following example.

$ /scripts/ftpupdate &
[1] 45218

Here, 45218 is the process ID of the program. To kill a process by Process ID, execute the following command.

$ kill 45218

It will immediately kill the process and stop it’s execution. So, This is how you can kill a process if it is running in the background. You can also find the process using ps command if you have lost the process ID of any execution.

So, this is How you can execute commands and programs and send them to the background. If you have any questions, please let us know in the comment section given below. We are happy to help!

Related guides

Account services are unavailable until the production backend is configured.