Score:1

Running program from a script upon resume from suspend

us flag

I try to run program RealTimeSync upon resuming system from sleep using script located in /lib/systemd/system-sleep/ in file RealTimeSync_kill_suspend.sh with following contents:

#!/bin/sh

case $1 in
        pre)
                echo "$(date) - $1: Killing RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                kill -9 `ps -aux | pgrep RealTimeSync`
                exit
                ;;
        post)
                echo "$(date) - $1: Invoking RealTimeSync resume script" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                sh /home/bart/Applications/FreeFileSync/RealTimeSync_resume.sh
;;
esac

I know that it executes sub-script RealTimeSync_resume.sh with the following contents:

#!/bin/sh

echo "$(date) - Running RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log

/home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real &

echo "$(date) - RealTimeSync should be running" >> /home/bart/Applications/FreeFileSync/suspend_resume.log

exit

as it drops lines after both echo statements in the RealTimeSync_kill_suspend.sh script and lines after both echo statements in RealTimeSync_resume.sh sub-script into a log file suspend_resume.log:

czw, 5 sie 2021, 16:55:50 CEST - pre: Killing RealTimeSync
czw, 5 sie 2021, 16:55:58 CEST - post: Invoking RealTimeSync resume script
czw, 5 sie 2021, 16:55:58 CEST - Running RealTimeSync
czw, 5 sie 2021, 16:56:28 CEST - RealTimeSync should be running

But when I look for the process with ps -aux | grep RealTimeSync it does not show any proper match, just:

bart       31262  0.0  0.0  12252  2612 pts/0    S+   17:38   0:00 grep --color=auto RealTimeSync

When I run sub-script with sh /home/bart/Applications/FreeFileSync/RealTimeSync_resume.sh I get correct process running with ps -aux | grep RealTimeSync:

bart       31066  0.0  0.0    212    68 pts/0    S    17:37   0:00 /home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real
bart       31071  0.3  0.1 442428 41260 pts/0    Sl   17:37   0:00 /home/bart/Applications/FreeFileSync/Bin/RealTimeSync_x86_64 /home/bart/Documents/Documents_backup.ffs_real
bart       31262  0.0  0.0  12252  2612 pts/0    S+   17:38   0:00 grep --color=auto RealTimeSync

All mentioned files have -rwxr-xr-x permissions.

After searching in forums I got a notion that RealTimeSync needs some service that is activated upon logon that is not available, for instance, people that had problems with running scripts starting synclients needed X server to connect to:

declare -x DISPLAY=":0.0"
declare -x XAUTHORITY="/home/<your user>/.Xauthority"
synclient VertEdgeScroll=1 VertTwoFingerScroll=1 HorizTwoFingerScroll=1 HorizEdgeScroll=1

from this forum-thread: https://ubuntuforums.org/showthread.php?t=2380045

I'd be grateful for any help.

EDIT 1

I found that "FreeFileSync and ReadTimeSync needs access to the graphical X11 display, so they can't be running via the system mode. In the user mode, systemd know about the user graphical session and use it." So there are potential 2 solutions:

Either one mentioned above, hardcoding DISPLAY and XAUTHORITY, discouraged as DISPLAY value may differ form differ form session to session.

Or running as user service, not system service, as display is initialized for user.

This I found here, troubleshooting running FreeFileSync as system service: https://unix.stackexchange.com/questions/529115/system-service-error-unable-to-initialize-gtk-is-display-set-properly

Unfortunately I have problems implementing these solutions, I'd be grateful for any help.

EDIT 2

Ok, I nailed it! I run RealTimeSync from the main script now, though I guess this does not make any difference, important is to initialize 'DISPLAY' as follows:

#!/bin/sh

case $1 in
        pre)
                echo "$(date) - $1: Killing RealTimeSync" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                kill -9 `ps -aux | pgrep RealTimeSync`
                exit
                ;;
        post)
                echo "$(date) - Invoking RealTimeSync resume script" >> /home/bart/Applications/FreeFileSync/suspend_resume.log
                env DISPLAY=:1 sudo -u bart /home/bart/Applications/FreeFileSync/RealTimeSync /home/bart/Documents/Documents_backup.ffs_real
;;
esac

And set owner of this script to root: chown root:root <script_name>

Solution was from here: start script after resume as logged in user (not root)

As I understand this is rather a way around than proper solution as hardcoding DISPLAY might pose problems, but temporarily works. In case anyone knows how to properly run this as a user not root, I'd be grateful for any hints.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.