Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there suspend\resume signals in Linux?

My application needs to react on hibernation mode so it can do some action on suspending and other actions on resuming. I've found some distributive-specific ways to achieve it(Upower + DBus) but didn't find anything universal. Is there a way to do it?

Thanks!

like image 294
ixSci Avatar asked May 26 '11 09:05

ixSci


People also ask

How do I suspend a resume thread in Linux?

Signal SIGUSR1 suspends the thread by calling pause() and SIGUSR2 resumes the thread. From the man page of pause: This example is problematic for numerous reasons. It is not thread safe: you do not wait until thread1 and thread2 set their signal handlers before calling pthread_kill.

How do you suspend a task in Linux?

You can (usually) tell Unix to suspend the job that is currently connected to your terminal by typing Control-Z (hold the control key down, and type the letter z). The shell will inform you that the process has been suspended, and it will assign the suspended job a job ID.

Can you pause 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.

How do you stop a resume process?

First, find the pid of the running process using ps command. Then, pause it using kill -STOP <PID> , and then hibernate your system. Resume your system and resume the stopped process using command kill -CONT <PID> .


Video Answer


2 Answers

A simple solution to this is to use a self-pipe. Open up a pipe and periodically write timestamps to it. select on this pipe to read the timestamps and compare them to the current time. When there is a big gap, that means you have just woken up from system suspension or hibernate mode.

As for the other way around, there is not much time when the lid is closed and it flips the switch.

If you really need to act on suspend, then you will need to set powersave hooks like this https://help.ubuntu.com/community/PowerManagement/ReducedPower in pm-utils. It could be as simple as

kill -1 `cat mypid` ; sleep 1

Your process would then trap SIGHUP and do what needs to be done to prepare for suspension. The sleep delays the process long enough for your program to react to the signal.

like image 105
Michael Dillon Avatar answered Oct 16 '22 08:10

Michael Dillon


I believe you are looking for SIGSTOP and SIGCONT signals. You can send these to a running process like so:

kill -STOP pid
sleep 60
kill -CONT pid
like image 2
macetw Avatar answered Oct 16 '22 08:10

macetw