Linux start program in background

How can I put the current running linux process in background? [closed]

I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish. How can I put that running program in background? So that I can still work on shell and that process also gets completed?

The command is already running so i dont have other option. I am not sure which command to try. i didn’t wanted to break the current process so i didn’t experimented it

We should wait a more professional answer then 🙂 I meant if you had the chance to start all over again. ( The command & thing)

The accepted answerer on this question explains the three steps which needs to be taken: stackoverflow.com/questions/625409/…

You can also just open a second instance of putty and connect to the server again to get another shell. Though the solution with ctrl+z is great.

1 Answer 1

Suspend the process with CTRL+Z then use the command bg to resume it in background. For example:

sleep 60 ^Z #Suspend character shown after hitting CTRL+Z [1]+ Stopped sleep 60 #Message showing stopped process info bg #Resume current job (last job stopped) 

More about job control and bg usage in bash manual page:

JOB CONTROL
Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash. [. ] The user may then manipulate the state of this job, using the bg command to continue it in the background, [. ]. A ^Z takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

bg [jobspec . ]
Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell’s notion of the current job is used.

To start a process where you can even kill the terminal and it still carries on running

nohup [command] [-args] > [filename] 2>&1 & 
nohup /home/edheal/myprog -arg1 -arg2 > /home/edheal/output.txt 2>&1 & 

To just ignore the output (not very wise) change the filename to /dev/null

To get the error message set to a different file change the &1 to a filename.

In addition: You can use the jobs command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1 or kill %2 with the number being the index of the process.

Читайте также:  Linux grant root privileges to user

Источник

Running Linux Commands in Background and Foreground

Learn how to run commands in background in Linux. You’ll also learn how to bring the background jobs back to foreground.

If you have a long-running task, it’s not always wise to wait for it to finish. I mean why keep the terminal occupied for a particular command? In Linux, you can send a command or process to the background so that the command would be running but the terminal will be free for you to run other commands.

In this tutorial, I’ll show you a couple of ways to send a process in the background. I’ll also show you how to bring the background processes back to the foreground.

Start a Linux process in the background directly

If you know that the command or process is going to take a long time, it would be a better idea to start the command in the background itself.

To run a Linux command in the background, all you have to do is to add an ampersand (&) at the end of the command, like this:

Let’s take a simple bash sleep command and send it to the background.

When the command finishes in the background, you should see information about that on the terminal.

Send a running Linux process to the background

If you already ran a program and then realized that you should have run it in the background, don’t worry. You can send a running process to the background as well.

What you have to do here is to use Ctrl+Z to suspend the running process and then use ‘bg‘ (short for background) to send the process in the background. The suspended process will now run in the background.

Let’s take the same example as before.

[email protected]:~$ sleep 60 ^Z [1]+ Stopped sleep 60 [email protected]:~$ bg [1]+ sleep 60 &

See all processes running in the background

Now that you know how to send the processes in the background, you might be interested in knowing which commands are running in the background.

For this purpose, you can enter this command in the terminal:

Let’s put some commands in the background first.

Now the jobs command will show you all the running jobs/processes/commands in the background like this:

jobs [1] Running firefox & [2]- Running gedit & [3]+ Stopped vim

Do you notice the numbers [1], [2] and [3] etc? These are the job ids. You would also notice the – and + sign on two of the commands. The + sign indicates the last job you have run or foregrounded. The – sign indicates the second last job that you ran or foregrounded.

Bring a Process to Foreground in Linux

Alright! So you learned to run commands in the background in Linux. But what about bringing a process running in the background to the foreground again?

Читайте также:  Что такое виртуальная машина линукс

To send the command to the background, you used ‘bg’. To bring the background process back, use the command ‘fg’.

Now if you simply use fg, it will bring the last process in the background job queue to the foreground. In our previous example, running ‘fg’ will bring Vim editor back to the terminal.

If you want to bring a certain process to the foreground, you need to specify its job id. The job id is the number you see at the beginning of each line in the output of the ‘jobs’ command.

Where n is the job id as displayed in the output of the command jobs.

This was a quick one but enough for you to learn a few things about running commands in the background in Linux. I would advise learning nohup command as well. This command lets you run commands in the background even after you log out of the session.

If you have questions or suggestions, please leave a comment below.

Источник

Send a Process to Background Linux

When working with graphical desktop environments, we rarely worry about background processes. If we have a process running in the foreground, we can quickly spawn another terminal window and continue with our work.

However, if you are in a raw terminal shell such as SSH, you will often feel concerned about processes that occupy and block the shell until they are completed, especially on long-running jobs. That is where the concept of background and foreground processes comes into play.

This tutorial will discuss what background and foreground processes are, including creating and managing them in Linux.

What is a Process?

Allow me to start at the basic level: what is a process?

In Linux, a process is an instance of a program. Typically, this means any command or executable in a shell is a process.

There are mainly two types of processes:

Foreground processes are mainly typical applications that we launch and interact with them. An example would be the nautilus file manager in Gnome. In most cases, we can start foreground processes from the shell or the desktop environment.

On the other hand, background processes run in the background and require no input or interaction from the user. An example would be any typical Linux daemon.

How to Run a Process in the Background

Suppose we have a process that, while running, occupies the shell session and hinders us from executing commands until it exits.

For example, if we run the Firefox browser in the shell, it will occupy the session until process termination.

As you can see, as long as Firefox is running, the shell prompt is unavailable, and we cannot execute any more commands.

To solve this, we can do it two ways:

1: Using an Ampersand (&)

The first method is using the ampersand & sign. This tells the shell to run whatever command precedes the ampersand in the background.

Читайте также:  Linux вызов системного монитора

In such a scenario, the process executes in the background and spawns as a new shell prompt allowing us to continue executing commands.

It also gives two numerical identifiers. The first one enclosed in square brackets is the Job ID, while the next one is the process ID.

2: Using CTRL + Z, bg command.

The next method you can use to put a process in the background is to use the shortcut CTRL + Z. This stops the process from blocking the shell. You can then use the bg command to push it to the background.

For example, start by launching Firefox as:

While the process is running, press CTRL + Z. This returns your shell prompt. Finally, enter the bg command to push the process in the background.

How to Show Background Processes

To view and manage processes in the background, use the jobs command in the shell. That will show the background jobs in the current terminal session.

An example output of background jobs:

To bring a process running in the background to the foreground, use the fg command followed by the job id.

For example, to bring the firefox job in the foreground, we use the command:

To put in the background again, press CTRL + Z followed by the bg command.

How to Make a Process Persistent After Shell Dies

When you are running processes in the background, and your shell session dies, all the processes associated with it terminate, which can be problematic, especially if it is an SSH session.

However, this is not too big an issue if you use a terminal multiplexer such as tmux or screen because, in that case, you can simply reattach the session.

However, if you run a shell session without a multiplexer, you can use the nohup command.

The nohup command is immune to hang-ups and can ignore the SIGHUP signal sent to a process.

Hence, if you run a command with nohup, it continues to run even if the shell session accidentally dies.

For example, to run Firefox with nohup, use the command:

This will run the process in the background as persist a shell terminate.

You can run a new terminal session and view the background jobs. You will see the process still running in the background.

Conclusion

In this tutorial, we discussed various ways to run and send processes to the background in Linux. We also covered how to bring a background process to the background and persist hang-up upon shell termination.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Оцените статью
Adblock
detector