Suspend process in linux

How To Suspend A Process And Resume It Later In Linux

Picture this scenario. You run a program. But, you don’t know how long it will take to finish. The process keeps running several minutes. You can’t wait that much longer, because some other important programs are waiting in the queue. Have you ever been in a situation like this? No worries! I just found a simple trick to suspend a process and resume it later in Linux.

What I am going to do is just pause the currently running process, do some other important tasks, and then resume the stopped process after all other processes are completed. This can be very useful when you have less RAM or Processor to do multi-task. You can pause the running processes at any time, and resume them later, without having to start them all over again. Now let us go ahead and learn to suspend or pause a running process and resume it later in Linux and Unix-like operating systems.

Suspend a process and resume it later in Linux

This is absolutely an easy! All you have to do is find the PID (Process ID) and using ps or ps aux command, and then pause it, finally resume it using kill command.

Let us see an example. I am going to download Ubuntu 18.04 netboot image using wget command:

Here, & symbol will move the running task (i.e. wget ) to the background without closing it.

Now, I want to pause this task and run other important tasks. To do so, first find the running processes using command:

Sample output:

As you see, the PID of wget command is 16143.

Let us stop this process now. To do so, run the following command from your Terminal:

Verify whether the process has been stopped or not using command:

Sample output:

See? The wget process has been stopped.

Go ahead and do other important tasks. Once all tasks are completed, resume the stopped process using command:

To verify it if the process is running, run ps command.

Suspend a process and resume it later in Linux

See? The process which we stopped earlier has been resumed!

Like I said already, this will be helpful if you can’t do multi-task in system that has low RAM or CPU speed.

First, find the pid of the running process using ps command. Then, pause it using kill -STOP

, and then hibernate your system. Resume your system and resume the stopped process using command kill -CONT

Does this work after restarting my system?

You might wonder, will this also work after a full system shutdown or reboot? NO. Because, the PIDs of the processes will automatically change after rebooting your system. They do not persist across reboot. In such cases, you can suspend or hibernate your entire system, and resume them when you are ready.

And, that’s all. You know now how to pause and resume a process in Linux and Unix-like operating systems. Hope this helps.

Источник

  • Suspend

Help on software suspend

This page gathers bits of information about getting software suspend to work in Debian. Because the core system components change rapidly among Debian versions, software suspend works differently on different versions of Debians. This page is divided according to Debian versions from new to old.

For more reading material, see also the links at the bottom of this page about hibernate and suspend.

Debian Jessie and newer (8 and newer)

With systemd, pm-utils and its hooks are not used any more, instead there’s systemd-suspend. To suspend use:

In Gnome

Another option, under Gnome shell, is to simply press ALT before clicking the shutdown button in the user menu.

Читайте также:  Application linux run window

In KDE Plasma

KDE already has a suspend button in its normal shutdown menu, though it may instead be labelled as «Sleep» in Plasma 5.16 and newer. Cases where it might not appear are if powerdevil or upower aren’t installed. systemd is used if available, but it’s not required.

Note that if power management is suspended by an application, the system may not suspend automatically, even if configured otherwise in your Energy Saving settings. This is often done by media players, for instance, to keep the screen from dimming. The Battery and Brightness item in your system tray will let you know if power management is suspended, and what process is currently suspending it.

Disable suspend and hibernation

For systems which should never attempt any type of suspension, these targets can be disabled at the systemd level with the following:

To re-enable hibernate and suspend use the following command:

A modern alternative approach for disabling suspend and hibernation is to create /etc/systemd/sleep.conf.d/nosuspend.conf as

The above technique works on Debian 10 Buster and newer. See systemd-sleep.conf(5) for details.

If you just want to prevent suspending when the lid is closed you can set the following options in /etc/systemd/logind.conf:

Then run systemctl restart systemd-logind.service or reboot.

More information is available in the manpage: man logind.conf

Debian Wheezy (7)

A very notable change is that HAL is phased out. If you still have the hal package installed, you should remove it or it will interference with pm-utils during suspend.

If the suspend / resume works well on your system, you are lucky and no need to read anything on this page. Or else, the first step to debug is to enable debugging for pm-utils, who control the suspend and resume process.

Enabling Debugging for pm-utils

The log of suspend and resume processes are in file /var/log/pm-suspend.log. It contains moderately verbose information by default. More information can be enabled for debugging by inserting line export PM_DEBUG=true into the beginning of file /usr/lib/pm-utils/pm-functions.

Fixing corrupted video on resume

A very common issue found after the computer resumes is corrupted video (or black screen, or no LCD backlight). The first step is to check whether the system is still running, which can be simply done by pressing the Capslock button and check whether the Capslock LED is changing accordingly. If the system is still running, in most cases we need to add a video quirk for your video card.

Debian now has kernel mode setting (KMS) enabled by default for most Intel, nVidia and ATI video cards. But pm-utils’ video quirk does support KMS yet. So in most cases you should try disabling KMS first. The detail steps for your specific video card can be found on the KernelModesetting page.

After disabled KMS, if the video after resume still corrupts, you can try to suspend the system by using some video quirks. Read the manpage of the pm-suspend program for a very detailed explanation of all the quirks available, and try the combinations of them from command-line. If you successfully find one combination of quirks that works for your system, you can add them into /usr/lib/pm-utils/video-quirks to make them permanent. At the same time, please help to file a bug against the pm-utils package with a patch about your changes so it can benefit the mass.

A common issue found on systems upgrading from old versions of Debian is the enabling of quirk-s3-bios freezes the system during suspend. If your system freezes during suspend, check the pm-suspend.log carefully after enabled debugging and make sure quirk-s3-bios is not used.

Kernel testing facility

The kernel has a Suspend testing facility changelog.

Introduce sysfs attribute /sys/power/pm_test allowing one to test the suspend core code. Namely, writing one of the strings below to this file causes the suspend code to work in one of the test modes defined as follows: freezer test the freezing of processes devices test the freezing of processes and suspending of devices platform test the freezing of processes, suspending of devices and platform global control methods(*) processors test the freezing of processes, suspending of devices, platform global control methods and the disabling of nonboot CPUs core test the freezing of processes, suspending of devices, platform global control methods, the disabling of nonboot CPUs and suspending of platform/system devices

Читайте также:  Как установить драйвер для камеры для windows 10

(*) These are ACPI global control methods on ACPI systems

Then, if a suspend is started by normal means, the suspend core will perform its normal operations up to the point indicated by given test level. Next, it will wait for 5 seconds and carry out the resume operations needed to transition the system back to the fully functional state. Writing «none» to /sys/power/pm_test turns the testing off.

When open for reading, /sys/power/pm_test contains a space-separated list of all available tests (including «none» that represents the normal functionality) in which the current test level is indicated by square brackets.

The actual message (for googlers) are suspend debug: Waiting for 5 seconds.

Источник

Write in front

In order to clean up the sleep and wake-up process of the platform system, by learning the power management methods of other platforms, we can achieve the goal through a winding path.

I just took over the new platform, and the corresponding information is not much, it is easy to make the manpower dissatisfied; I searched for learning resources on the Internet and found that Wei Dongshan has relevant content on the driver of S3C2440, and the reputation is also good, which can be used as an entry point.

Different platforms must have platform differences, and there will be corresponding differences between different Linux kernel versions, but the ideas are the same, which can be summarized to help understand the development and use of power management.

This article summarizes the process and development methods of system sleep and wakeup.

Linux power management basic framework

The following figure shows the basic framework of Linux power management. For details, please refer toLinux power management (1)_overall architecture
This article focuses on the use of Generic PM.


Generic PM is Power Management, such as Power Off,
Suspend to RAM, Suspend to Disk, Hibernate, etc.

Regarding Suspend, the Linux kernel provides four types of Suspend: Freeze, Standby, STR (Suspend to RAM) and STD (Suspend to Disk),As follows . Write «freeze», «standby», «mem» and «disk» to the «/sys/power/state» file in the user space to trigger them.

mode description
freeze Freeze I/O devices, put them in a low-power state, make the processor enter an idle state, wake up the fastest, and consume more power than other standby, mem, and disk methods
standby In addition to freezing I/O devices, it also suspends the system, wakes up faster, and consumes more power than other mem and disk methods
mem Store the running status data in the memory, turn off the peripherals, enter the waiting mode, wake up slowly, and consume more power than the disk mode
disk Save the running status data to the hard disk, and then shut down, wake up the slowest. For embedded systems, because there is no hard disk, so generally do not support

The power management framework provided by the Linux kernel introduces object-oriented thinking, which is difficult to understand. For this reason, it is necessary to verify the results of suspend through experiments on the basis of putting aside the complex framework, and after summarizing the process of suspend, help understand the supend process of Linux.

S3C2440 Sleep mode entry and wake-up

There are three modes for the power manager of S3C2440, as shown in the figure below,

We choose to implement Sleep mode in u-boot as suspend, which is the mem mode suspend implemented by the Linux kernel. The effect of the experiment is: the system enters Sleep Mode after running the suspend command on the u-boot command line; when the button is pressed , The system wakes up and continues to execute from the place before sleep.

The process of entering Sleep Mode

The implementation code is as follows:

Code explanation

  1. For the fifth step of entering Sleep Mode, the main purpose is to save the PC pointer before sleep to the GSTATUS register to restore the scene after waking up. The realization of this step is placed in the s3c2440_cpu_suspend function.

The function of the s3c2440_cpu_suspend function is to make the system truly enter the dormant state. The implementation process is as follows:

Wake up process


From the second point in the above description, it can be known that the system needs to restart u-boot to exit from Sleep mode. The difference between power-up and wake-up is to read Take the GSTATUS2 register. If the value is 1, it means it came from Sleep Mode, and the corresponding process will be executed to restore the scene before sleep; otherwise, the normal startup process will be executed.

The following is about the modification of u-boot startup code

Experimental phenomena

Under the u-boot command line,
Enter suspend mode, sleep.

Press the button to exit the suspend mode and wake up.

S3C2440 Generic PM’s suspend

After understanding the process of adding sleep and wake up under S3C2440 u-boot, two pictures are summarized:


For the Generic PM framework of Linux, the essence is to place the above part related to the platform (platform) to the corresponding position of the framework (that is, the Platform dependent PM in the figure below) Position), you can implement suspend and resume under Linux.

Introduce the Generic PM framework, referenceLinux power management (6)_Generic PM’s Suspend function

Let’s take suspend to RAM as an example to analyze the process from the user space ehco> mem /sys/power/state to sleep and then the key to wake up.Linux power management

suspend process

As shown in the figure below, the function actually called to write a file is state_store.

The call process of sleep-related power management functions in the driver: prepare—>suspend—>suspend_late—>suspend_noirq
The bottom line in the figure: suspend_ops->enter is the code related to platform dependent PM. Just transplant the code that enters the sleep mode in u-boot.
The corresponding platform depend PM code in S3C2440 is in arch/arm/plat-samsung/pm.c

Code description:
Among them, line 27-32, check whether the wake-up source is set, if not, exit directly, and enter the sleep mode is prohibited. For setting the wake-up source, how to set the wake-up will be described in the key driver later source.
Lines 34-67, perform the corresponding work according to the process of entering Sleep Mode. Once the call to line 67, jump to pm_cpu_sleep, the system enters the sleep state. It will not wake up until there is a wake-up source. Continue to execute.

resume process


The calling process of wake-up related power management functions in the driver: resume_noirq—>resume_early—>resume->complete
For lines 71-90 of the code in arch/arm/plat-samsung/pm.c above, it is to complete the resume process in the above figure.

Modify the key driver to add a wake-up source

Code description
The most important thing to set the wake-up source is the function irq_set_irq_wake in lines 105-107.
This function essentially enables the interrupt function through the api setting register. (Note: These interrupt pins support the wake-up function)

Device PM (for each driver)

The above implementation of the suspend process is to modify the code related to Platform dependent PM (for CPU chip related), and only execute the necessary requirements in the datasheet during sleep. Some peripherals, such as network cards, sound cards, etc., are not turned off.

For peripheral-level power management, the Linux Generic PM framework provides corresponding interfaces. As long as the corresponding parameters are added when registering the device, the suspend or resume callback functions registered by each driver can be called when the system sleeps or wakes up.

Let’s take LCD as an example, add peripheral power management, so that after the system enters Mem sleep mode, the LCD backlight can be turned off, and the LCD backlight can be turned on to display again when the system wakes up.

Code analysis

  1. A platform device and driver are registered on the basis of the LCD driver. When the system enters the sleep mode, it will call the lcd_suspend function to turn off the light, and when the system wakes up and exits the sleep mode, it will call lcd_resume to turn on the light.
  2. The focus is on the structure in line 13.

to sum up

The above is the power management of the system in sleep or wake mode. For different platforms, you need to select the sleep mode according to the instructions of DataSheet, and modify u-boot and kernel.
Then in the normal operation mode, can the device be managed separately, please look forward to the next updateLinux Power Management-Runtime PM

Источник

Читайте также:  Установка google drive linux
Оцените статью