]> granicus.if.org Git - fcron/blob - script/fcron.suspend.sh.in
Improve support for hardware suspend (to memory or to disk)
[fcron] / script / fcron.suspend.sh.in
1 #!/bin/sh
2 #
3 # Suspend script for fcron.
4 # Designed to work under systemd and pm-utils.
5 #
6 # Install as:
7 # - systemd: /usr/lib/systemd/system-sleep/fcron.sh
8 # - pm-utils: /etc/pm/sleep.d/74_fcron
9 #
10
11 PID_FILE=/usr/local/var/run/fcron.pid
12 SUSPEND_FILE=/usr/local/var/run/fcron.suspend
13 PATH=/bin:/usr/bin:/sbin:/usr/sbin
14 LOGGER="logger -p cron.info"
15
16 FCRONPID=`cat $PID_FILE`
17
18 # pm-utils: first argument will be hibernate|suspend|resume|thaw
19 # systemd: two arguments: 1st one is pre|post, second one is suspend|hibernate|hybrid-sleep
20
21 case $1 in
22      pre|suspend|suspend_hybrid|hibernate)
23         # We will use the modify time of SUSPEND_FILE to know when we went into suspend:
24         kill -STOP "$FCRONPID" || $LOGGER "$0 $*: could not stop fcron pid '$FCRONPID'"
25         touch $SUSPEND_FILE || $LOGGER "$0 $*: could not touch fcron suspend file '$SUSPEND_FILE'"
26         $LOGGER "$0 $*: stopped pid `cat $PID_FILE` (from $PID_FILE), and touched $SUSPEND_FILE"
27         ;;
28      post|resume|thaw)
29         SLEEP_FROM=`stat -c %Y $SUSPEND_FILE`
30         if test $? -eq 0; then
31             NOW=`date +%s`
32             SLEEP_DURATION=`expr $NOW - $SLEEP_FROM`
33             if test $? -lt 2; then
34                 $LOGGER "$0 $*: SLEEP_DURATION=$SLEEP_DURATION"
35                 echo $SLEEP_DURATION > $SUSPEND_FILE
36             else
37                 # something went wrong -- resume fcron without specifying
38                 # a suspend duration as it may be wrong
39                 $LOGGER "$0 $*: could not compute sleep duration"
40                 rm -f $SUSPEND_FILE
41             fi
42         else
43             # something went wrong -- resume fcron without specifying
44             # a suspend duration as it may be wrong
45             $LOGGER "$0 $*: could not stat $SUSPEND_FILE"
46             rm -f $SUSPEND_FILE
47         fi
48         $LOGGER "$0 $*: resuming pid `cat $PID_FILE` (from $PID_FILE)"
49         kill -CONT `cat $PID_FILE` || $LOGGER "$0 $*: could not resume fcron pid '$FCRONPID'"
50         ;;
51     *)
52         $LOGGER "$0 $*: invalid argument."
53 esac