Linux run stopped jobs

How can I resume a stopped job in Linux?

This is actually a fairly normal work flow for Vim, if you want to keep you commands in your bash history, then you hit Ctrl-z type your commands and then resume. Obviously you can run commands without leaving Vim via the :! ed command

5 Answers 5

The command fg is what you want to use. You can also give it a job number if there are more than one stopped jobs.

The general job control commands in Linux are:

  • jobs — list the current jobs
  • fg — resume the job that’s next in the queue
  • fg %[number] — resume job [number]
  • bg — Push the next job in the queue into the background
  • bg %[number] — Push the job [number] into the background
  • kill %[number] — Kill the job numbered [number]
  • kill -[signal] %[number] — Send the signal [signal] to job number [number]
  • disown %[number] — disown the process(no more terminal will be owner), so command will be alive even after closing the terminal.

That’s pretty much all of them. Note the % infront of the job number in the commands — this is what tells kill you’re talking about jobs and not processes.

Why use «%» character. Is it required to be prepended before the job number or Is it a unix convention to specify the int type ?

You can also type % ; i.e., you hit Ctrl-Z in emacs, then you can type %emacs in the console and bring it back to the foreground.

Just to add to the other answers, bash lets you skip the fg if you specify a job number.

For example, these are equivalent and resume the latest job:

% is awesome, thanks! As a touch-typist, I find fg very irritating (same finger). But then, so is cd .

in my case when i tried to use fg i see the stopped process appears and disappears quickly and just succeeded to restore it.

If you didn’t launch it from current terminal, use ps aux | grep to find the process number (pid), then resume it with:

(Despite the name, kill is simply a tool to send a signal to the process, allowing processes to communicate with each other. A «kill signal» is only one of many standard signals.)

Bonus tip: wrap the first character of the process name with [] to prevent the grep command itself appearing in the results. e.g. to find emacs process, use ps aux | grep [e]macs

Источник

There are stopped jobs (on bash exit)

Bash did not exit, I must exit again to exit the bash shell.

  • Q: What is a ‘stopped job’, or what does this mean?
  • Q: Can a stopped process be resumed?
  • Q: Does the first exit kill the stopped jobs?
  • Q: Is there a way to exit the shell the first time? (without entering exit twice)
Читайте также:  File type command in linux

2 Answers 2

A stopped job is one that has been temporarily put into the background and is no longer running, but is still using resources (i.e. system memory). Because that job is not attached to the current terminal, it cannot produce output and is not receiving input from the user.

You can see jobs you have running using the jobs builtin command in bash, probably other shells as well. Example:

user@mysystem:~$ jobs [1] + Stopped python user@mysystem:~$ 

You can resume a stopped job by using the fg (foreground) bash built-in command. If you have multiple commands that have been stopped you must specify which one to resume by passing jobspec number on the command line with fg . If only one program is stopped, you may use fg alone:

At this point you are back in the python interpreter and may exit by using control-D.

Conversely, you may kill the command with either it’s jobspec or PID. For instance:

user@mysystem:~$ ps PID TTY TIME CMD 16174 pts/3 00:00:00 bash 17781 pts/3 00:00:00 python 18276 pts/3 00:00:00 ps user@mysystem:~$ kill 17781 [1]+ Killed python user@mysystem:~$ 

To use the jobspec, precede the number with the percent (%) key:

user@mysystem:~$ kill %1 [1]+ Terminated python 

If you issue an exit command with stopped jobs, the warning you saw will be given. The jobs will be left running for safety. That’s to make sure you are aware you are attempting to kill jobs you might have forgotten you stopped. The second time you use the exit command the jobs are terminated and the shell exits. This may cause problems for some programs that aren’t intended to be killed in this fashion.

In bash it seems you can use the logout command which will kill stopped processes and exit. This may cause unwanted results.

Also note that some programs may not exit when terminated in this way, and your system could end up with a lot of orphaned processes using up resources if you make a habit of doing that.

Note that you can create background process that will stop if they require user input:

user@mysystem:~$ python & [1] 19028 user@mysystem:~$ jobs [1]+ Stopped python 

You can resume and kill these jobs in the same way you did jobs that you stopped with the Ctrl-z interrupt.

Источник

What should I do when I get ‘There are stopped jobs’ error?

enter image description here

I face this type of situation many times. For example, whenever I try to open some file in emacs with sudo rights using: sudo emacs tet.c & Instead of asking me for the password Ubuntu just starts emacs process without any emacs window or any output on terminal (except for the pid) see the image (if I don’t use ‘&’ then it will ask me for the password): I have two questions related with this:

  1. What should I do when I get error that ‘There are stopped jobs’? How do I identify all such stopped jobs and kill them? Once I clear the terminal I won’t have pids of these stopped processes.
  2. Why is Ubuntu/emacs behaving like this? Why doesn’t it ask me for the password?
Читайте также:  Командная строка linux полное руководство уильям шотс

3 Answers 3

There are stopped jobs message is far, far away to be an error. It’s just a notification telling you that you attempt to exit from the shell, but you have one or more suspended jobs/programs (in your case emacs which you putted in background using & at the end of your command). The system doesn’t let you to exit from the shell and kill the jobs unless you mean to. You can do a couple of things in response to this message:

  • use jobs command to tell you what job(s) you have suspended
  • you can choose to add the job(s) in the foreground using fg command
  • if you don’t care if the job(s) will terminate, you can just type exit again; typing exit a second time with or without an intervening jobs command will result in the termination of all suspended jobs.

To answer the second question, I will tell you that not Ubuntu or emacs behaving like this. This is a normal behavior when you put an application to run in background. In this case sudo is asking for password, but is asking in background, so you can’t see this fact. To see it, you should bring back the job in foreground using fg command:

radu@Radu: ~ $ sudo emacs tet.c & [1] 7732 radu@Radu: ~ $ # now sudo emacs run in background so you can't see nothing about what's happening radu@Radu: ~ $ fg [sudo] password for radu: 

After this you can type Ctrl + Z to put again the job in background if you want. Then you can run again ‘fg’ command to bring back the job in foreground and so on.

Источник

Starting Stopped Jobs in Ubuntu Linux

Starting Stopped Jobs in Ubuntu Linux

Type the “jobs” command at a terminal prompt and press “Enter” to view information about stopped, running and terminated jobs for your current user session. The jobs command displays information about the jobs in three columns. The first column contains the job’s number, the second column contains the job’s status and the last column contains the command the job is running.

A job’s status can be either “Running” if it’s currently running, “Stopped” if it’s stopped or “Killed” if you’ve terminated it.

You can use the number displayed in the first column to perform actions on a specific job, such as starting it in the background, starting it in the foreground or terminating it.

Starting Jobs

Starting a stopped job in the foreground.

You can type “fg” into the Ubuntu terminal and press “Enter” to start the most recently stopped job in the foreground. A job started in the foreground displays its output in the terminal window. You can also type “bg” instead to start the most recently stopped job in the background. A job running in the background doesn’t display any output in the terminal, leaving your terminal session free to perform other commands.

You can also use the “fg” or “bg” commands to start specific jobs in the foreground or background. Type “fg %1” or “bg %1” into the terminal to start a stopped job in the foreground or background, replacing “1” with the number of the stopped job from the jobs command, and press “Enter.”

Читайте также:  Kali linux среды разработки

Stopping Jobs

Once the job is started in the foreground, you can stop it again by pressing the “Ctrl” and “Z” keys on your keyboard at the same time, or terminate it entirely by pressing the “Ctrl” and “C” keys on your keyboard at the same time.

If a job is running in the background, you can use the “fg %1” command to bring it to the foreground, replacing “1” with the number of the job from the jobs command.

You can terminate a job with the kill command. Type “kill -9 %1” into the terminal, replacing “1” with the job’s number from the jobs command, and press “Enter” to kill the job. You’ll lose any open data the job hasn’t saved yet because the “kill -9” command forcibly terminates processes without allowing them to save any open data.

Logging Out

If you type the “logout” or “exit” commands into a terminal and receive the “There are stopped jobs” message, you don’t have to stop the jobs before logging out. This is a confirmation method to remind you that there are stopped jobs you will lose if you log out. You can continue with the log-out process and forcibly terminate the stopped jobs by typing the “logout” command into the terminal again and pressing “Enter” without typing the “jobs” command in between to list the stopped jobs. If you type the “jobs” command in between, you’ll have to type the “logout” command twice.

Upstart Jobs

Starting an Upstart job, also known as a service.

Ubuntu also includes the Upstart daemon, which replaces the traditional init daemon and manages the services running in the background on your Ubuntu system. Upstart also refers to the services installed on the system as jobs. You can use the “start” and “stop” commands to start stopped services and stop running services.

Type “sudo start service” into the terminal window, replacing “service” with the name of the stopped Upstart job you want to start, and press “Enter” to start a stopped upstart Job. You can also use the “sudo stop service” command to stop a running service.

The sudo command will prompt you for your password. Type it at the password prompt and press “Enter” to authenticate.

If you don’t know the exact name of the Upstart job you want to start, you can type “ls /etc/init.d” into the terminal and press “Enter” to see a list of Upstart jobs stored in the init.d directory.

Starting Jobs in the Background

By default, any command you type in the terminal runs as a foreground job. You can directly run a command as a background job by appending it with a space and an ampersand, such as “firefox &” to run Firefox as a background job. The Firefox browser window will open on your desktop, but your terminal won’t show Firefox’s status messages.

References

  1. UC San Diego: Job Control on UNIX Systems
  2. University of California, Irvine: There Are Stopped Jobs
  3. Upstart: Getting Started
  4. Author’s own experience.
  5. Screenshots provided by writer.

Источник

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