Suspend process in linux

linux: suspend process at startup

I would like to spawn a process suspended, possibly in the context of another user (e.g. via sudo -u . ), set up some iptables rules for the spawned process, continue running the process, and remove the iptable rules when the process exists. Is there any standart means (bash, corutils, etc.) that allows me to achieve the above? In particular, how can I spawn a process in a suspended state and get its pid?

Why do you need to start the process before configuring the environment? This sound like a typical use of fork/exec combo.

@hanshans You didn’t answer my question. I’m asking why you need to start the process before you will configure it. The normal process is to configure the environment and then start the process. This just seems weird.

@Robin Well, that is still normal fork/exec scenario. But never mind, I’m obviously missing the point.

5 Answers 5

Write a wrapper script start-stopped.sh like this:

#!/bin/sh kill -STOP $$ # suspend myself # . until I receive SIGCONT exec $@ # exec argument list 
sudo -u $SOME_USER start-stopped.sh mycommand & # start mycommand in stopped state MYCOMMAND_PID=$! setup_iptables $MYCOMMAND_PID # use its PID to setup iptables sudo -u $SOME_USER kill -CONT $MYCOMMAND_PID # make mycommand continue wait $MYCOMMAND_PID # wait for its termination MYCOMMAND_EXIT_STATUS=$? teardown_iptables # remove iptables rules report $MYCOMMAND_EXIT_STATUS # report errors, if necessary 

All this is overkill, however. You don’t need to spawn your process in a suspended state to get the job done. Just make a wrapper script setup_iptables_and_start :

#!/bin/sh setup_iptables $$ # use my own PID to setup iptables exec sudo -u $SOME_USER $@ # exec'ed command will have same PID 
setup_iptables_and_start mycommand || report errors teardown_iptables 

Источник

How to Suspend a Process in Linux

Process suspension or suspend a job refers to a process that has been turned off from the running state. Although the process exists, it is not scheduled for execution. Consider a server on which you wish to run CPU-intensive experimental and computational programs that will take two months to complete.

Why Do We Need to Suspend a Process?

You can suspend a process for a variety of reasons. The most important of which is when a large number of interactive requests come in for processing. The memory management system swaps the process out of memory to make space for other processes.

Читайте также:  Linux mint cursor theme

We will demonstrate in this article how to kill or suspend a process in a Linux system.

Suspend a Process in Linux

It is quite an easy job to suspend a process in Linux. In UNIX, by typing ‘Ctrl+Z’, you can suspend the job that is currently connected to your terminal. The command prompt will notify you that the process has been stopped. It will assign a process ID to the suspended job.

If you want to run this process in the background, then type the ‘bg’ command:

When there is no other option available to suspend a process, we can stop it manually by using the terminal.

To suspend or terminate a process, first, you will find the PID (process ID) using the ‘pd’ command. Once you found the process id, you can suspend this job by using the kill, pkill, killall, or top commands.

Example

For example, we want to download a Joomla full stable package via the wget command using the terminal.

$ wget https: // downloads.joomla.org / cms / joomla3 / 3 — 8 — 5 / Joomla_3- 8 — 5 -Stable-Full_Package.zip &

It takes much time to complete as compared to other processes. To check the process id, use the following command:

You can also find the process id for a specific running job using this command:

If multiple files are downloading in the background of your system, use this command:

Once you found the process id, you can suspend the job using the process id using the following ‘kill’ command:

For example, we want to kill or suspend a job having process id ‘5562’. In this case, use this command:

If you want to run this process again, use this command:

View the running process id by using the following command:

Conclusion

We discussed in this post how to suspend a process in Linux using two methods: the keyboard shortcut and through the terminal. However, if you restart your system, this technique will not work. When you reboot your system, the process ids of all processes will automatically change.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

How to Suspend and Resume Processes in Linux [Quick Tip]

Learn how to suspend a running process in the Linux command line. Also learn how to resume a stopped process.

Have you ever encountered a situation where you have to suspend an ongoing task as you have more important tasks to execute and want to resume the previous task later?

Читайте также:  Linux check dir size

Actually, it is possible and can be done pretty easily using two termination signals: STOP and CONT .

And in this guide, I will walk you through step-by-step to suspend and resume the process again in Linux.

Suspending the process in Linux

You have two options to suspend the process:

  1. Using the Ctrl + Z shortcut (for a process running in the foreground)
  2. Using the kill command with the STOP signal

A suspended process is denoted as stopped in the terminal. This may confuse you but the ‘stopped process’ can be resumed. After all, there is a difference between stop (SIGSTOP) and termination (SIGTERM).

1. Suspend a foreground process

Let me give you an example you can see. I run Firefox from the command line with this command:

The browser runs and you can see some strange output on the terminal. That’s okay.

No, I suspend the Firefox process which is running in the foreground with the Ctrl+z terminal shortcut:

And here is what happens. The Firefox process is suspended and you can see it in the terminal. After a few seconds of delay, a dialogue box popups to notify that the Firefox browser is not responding anymore.

Example of suspending a process in Linux

2. Suspend a process by sending STOP signal

When the process is running in the background, you can not use the Ctrl + Z shortcut. In that case, you’d have to use the kill command.

Let’s see this with an example. I run the sleep command for a really long time. But I am running the command in the background by adding & to it.

You can verify the background processes using the jobs command:

list background processes

I can see the process ID in the example. There are various ways to find process ID. You can use the ps command and grep on the process name.

Once you have the process ID, you can suspend the process using the kill command in the following manner:

In my case, the PID is 26589, then, I’d have to use the following command:

terminate the process using kill command in terminal

And as you can see, the process is stopped successfully.

Resuming the process again

To resume the process, you’d have to use the CONT flag with the kill command as shown:

In my case, the PID was 26589 then, the command would be:

resume the terminated process in Linux

And as you can see, after resuming the process, I used the jobs command, which showed the process was running as it should.

Know more about termination signals

Apart from STOP and TERM signals which I explained above, the kill command offers various other termination signals to kill the process with different effects.

Читайте также:  Узнать внешний ip адрес linux консоль

And we made a detailed guide for how you can use the most popular ones:

I hope you will find this quick tip helpful.

Источник

How to suspend and resume processes

In the bash terminal I can hit Control + Z to suspend any running process. then I can type fg to resume the process. Is it possible to suspend a process if I only have it’s PID? And if so, what command should I use? I’m looking for something like:

suspend-process $PID_OF_PROCESS 
resume-process $PID_OF_PROCESS 

2 Answers 2

You can use kill to stop the process.

For a ‘polite’ stop to the process (prefer this for normal use), send SIGTSTP :

For a ‘hard’ stop, send SIGSTOP :

Note that if the process you are trying to stop by PID is in your shell’s job table, it may remain visible there, but terminated, until the process is fg ‘d again.

To resume execution of the process, sent SIGCONT :

Unless there are other reasons for it, I would prefer SIGTSTP over SIGSTOP, as some applications do handle SIGTSTP specially. For example, if scp is showing a progress bar, SIGTSTP will cause it to clean up the terminal mode before suspending, but if you send SIGSTOP, it will not have a chance to do so.

@ephemient I tried SIGTSTP, I saw what you were saying about it cleaning up the terminal. Thanks for the explanation of SIGTSTP, alawys good to learn new things 🙂

Also useful to note that you can reference the [pid] value by using the % symbol and then the job number (one that you can find by running jobs ). So you’d go: kill -TSTP %1

Why do you mean by it may remain visible there, but terminated ? It’s not terminated but stopped/paused. I tried kill -STOP and it behaves exactly like kill -TSTP for shell jobs.

You should use the kill command for that.

To be more verbose — you have to specify the right signal, i.e.

for suspending the process and

for resuming it. Documented at 24.2.5 Job Control Signals.

I wonder what accident of history led to this answer getting fewer votes? The answers are nearly the same and this one came first.

@Wildcard, when I created the answer I was a bit in a hurry, thus, it basically just contained the first part up to kill -TSTP (i.e. how to suspend). 1/2 year later, i.e. 2011, I revisited my answer and noticed its incompleteness. Thus, I edited it and added also the kill -CONT part. This should explain the difference in votes in comparison with Steve’s answer.

Источник

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