Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-11-02 Thread Richard Maw
On Mon, Nov 02, 2015 at 09:04:31AM +0100, Lennart Poettering wrote:
> On Wed, 28.10.15 19:30, John (da_audioph...@yahoo.com) wrote:
> 
> > I have a simple bash script that I would like to have a user service
> > file run with an argument when the system enters a sleep or
> > hibernation state but as I understand it, user service units do not
> > use the sleep.target.  The goal is to have the following run before
> > the system goes into sleep/hibernate triggered by whatever mechanism
> > systemd uses to detect when the user sleeps or hibernates the
> > system: '/usr/bin/psd sync'
> 
> You can install a suspend delay inhibitor:
> 
> https://wiki.freedesktop.org/www/Software/systemd/inhibit/
> 
> That works from privileged code the same as for user code. However,
> you cannot really do that from shell code. I fear for shell this is
> simply not available, sorry.

You may be able to abuse the hell out of systemd-inhibit to have it work:

#!/bin/sh
# Approach cribbed from http://www.opopop.net/Harnessing_DBus/
while true; do
# Hold inhibitor lock until dbus signal that preparing to sleep
systemd-inhibit --what=sleep --why="sync psd" sh -c '
dbus-monitor --system 
"interface='org.freedesktop.login1.Manager',member='PrepareForSleep'" |
while read -r line; do
read type value
if [ "$type" = boolean -a "$value" = true ]; then
break
fi
done
/usr/bin/psd sync
'
# Wait for a message saying we're resuming before restarting monitor
dbus-monitor --system 
"interface='org.freedesktop.login1.Manager',member='PrepareForSleep'" |
while read -r line; do
read type value
if [ "$type" = boolean -a "$value" = false ]; then
break
fi
done
done

Though I wouldn't honestly recommend doing it this way.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-11-02 Thread Mantas Mikulėnas
On Mon, Nov 2, 2015 at 3:34 PM, Richard Maw 
wrote:

> On Mon, Nov 02, 2015 at 09:04:31AM +0100, Lennart Poettering wrote:
> > On Wed, 28.10.15 19:30, John (da_audioph...@yahoo.com) wrote:
> >
> > > I have a simple bash script that I would like to have a user service
> > > file run with an argument when the system enters a sleep or
> > > hibernation state but as I understand it, user service units do not
> > > use the sleep.target.  The goal is to have the following run before
> > > the system goes into sleep/hibernate triggered by whatever mechanism
> > > systemd uses to detect when the user sleeps or hibernates the
> > > system: '/usr/bin/psd sync'
> >
> > You can install a suspend delay inhibitor:
> >
> > https://wiki.freedesktop.org/www/Software/systemd/inhibit/
> >
> > That works from privileged code the same as for user code. However,
> > you cannot really do that from shell code. I fear for shell this is
> > simply not available, sorry.
>
> You may be able to abuse the hell out of systemd-inhibit to have it work:
>
> #!/bin/sh
> # Approach cribbed from http://www.opopop.net/Harnessing_DBus/


Might as well use ctypes.sh then... Or a more capable language:

https://gist.github.com/grawity/a10ee46d7ff58048d483

-- 
Mantas Mikulėnas 
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-11-02 Thread Lennart Poettering
On Wed, 28.10.15 19:30, John (da_audioph...@yahoo.com) wrote:

> I have a simple bash script that I would like to have a user service
> file run with an argument when the system enters a sleep or
> hibernation state but as I understand it, user service units do not
> use the sleep.target.  The goal is to have the following run before
> the system goes into sleep/hibernate triggered by whatever mechanism
> systemd uses to detect when the user sleeps or hibernates the
> system: '/usr/bin/psd sync'

You can install a suspend delay inhibitor:

https://wiki.freedesktop.org/www/Software/systemd/inhibit/

That works from privileged code the same as for user code. However,
you cannot really do that from shell code. I fear for shell this is
simply not available, sorry.

Lennart

-- 
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-11-02 Thread Richard Maw
On Mon, Nov 02, 2015 at 04:11:14PM +0200, Mantas Mikulėnas wrote:
> On Mon, Nov 2, 2015 at 3:34 PM, Richard Maw 
> wrote:
> 
> > On Mon, Nov 02, 2015 at 09:04:31AM +0100, Lennart Poettering wrote:
> > > On Wed, 28.10.15 19:30, John (da_audioph...@yahoo.com) wrote:
> > >
> > > > I have a simple bash script that I would like to have a user service
> > > > file run with an argument when the system enters a sleep or
> > > > hibernation state but as I understand it, user service units do not
> > > > use the sleep.target.  The goal is to have the following run before
> > > > the system goes into sleep/hibernate triggered by whatever mechanism
> > > > systemd uses to detect when the user sleeps or hibernates the
> > > > system: '/usr/bin/psd sync'
> > >
> > > You can install a suspend delay inhibitor:
> > >
> > > https://wiki.freedesktop.org/www/Software/systemd/inhibit/
> > >
> > > That works from privileged code the same as for user code. However,
> > > you cannot really do that from shell code. I fear for shell this is
> > > simply not available, sorry.
> >
> > You may be able to abuse the hell out of systemd-inhibit to have it work:
> >
> > #!/bin/sh
> > # Approach cribbed from http://www.opopop.net/Harnessing_DBus/
> 
> 
> Might as well use ctypes.sh then...

Heh, certainly. I'd forgotten that existed.

I mostly took the suggestion that it wasn't possible from shell as a challenge 
:-)
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-10-29 Thread Simon McVittie
On 29/10/15 18:52, John wrote:
> This is an interesting idea but I would like to learn about user units and 
> sleep mode :)

I think the intention is that per-user code deals with sleep by having a
service (daemon) that registers to inhibit suspend; when it is notified
that systemd would like to suspend, it does what it needs to do, then
releases the inhibit to allow suspend to continue.

Here's a working implementation (it also handles two flavours of network
connectivity, but you can ignore those bits), which Telepathy uses to
try to log out from IM services before suspending:
http://cgit.freedesktop.org/telepathy/telepathy-mission-control/tree/src/connectivity-monitor.c

-- 
Simon McVittie
Collabora Ltd. 

___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-10-29 Thread John

> From: David Timothy Strauss 
>To: John ; "systemd-devel@lists.freedesktop.org" 
> 
>Sent: Wednesday, October 28, 2015 7:54 PM
>Subject: Re: [systemd-devel] Help writing a user service file that will exec a 
>command upon system sleep
> 
>
>
>I read up on PSD, and I expect you're using it for performance/hardware 
>longevity. Correct me if I'm wrong about this.
>
>So, have you considered just mounting your home directory or a volume for 
>things like browser profile data with normally aggressive/unsafe options on a 
>normal file system? You can configure file systems like ext4 to only commit at 
>periodic intervals, and you can also configure your system to commit/sync all 
>file systems before going to sleep. They will buffer writes to memory until 
>committing, and reads will buffer into the kernel's page cache. This option is 
>not crash-safe, but neither is PSD.
>


This is an interesting idea but I would like to learn about user units and 
sleep mode :)
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-10-29 Thread John




- Original Message -
> From: Simon McVittie 
> To: systemd-devel@lists.freedesktop.org
> Cc: 
> Sent: Thursday, October 29, 2015 3:27 PM
> Subject: Re: [systemd-devel] Help writing a user service file that will exec 
> a command upon system sleep
> 
> On 29/10/15 18:52, John wrote:
>>  This is an interesting idea but I would like to learn about user units and 
> sleep mode :)
> 
> I think the intention is that per-user code deals with sleep by having a
> service (daemon) that registers to inhibit suspend; when it is notified
> that systemd would like to suspend, it does what it needs to do, then
> releases the inhibit to allow suspend to continue.
> 
> Here's a working implementation (it also handles two flavours of network
> connectivity, but you can ignore those bits), which Telepathy uses to
> try to log out from IM services before suspending:
> http://cgit.freedesktop.org/telepathy/telepathy-mission-control/tree/src/connectivity-monitor.c


Hello Simon.  Thank you for the post.  I am not a programmer and cannot really 
read the code you referenced.  Can you point me to a particular line number or 
line numbers?  Since my tool is a bash script, I'm looking to distill out if 
possible the relevant sections and adapt to this use case.  Thank you.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel


[systemd-devel] Help writing a user service file that will exec a command upon system sleep

2015-10-28 Thread John
I have a simple bash script that I would like to have a user service file run 
with an argument when the system enters a sleep or hibernation state but as I 
understand it, user service units do not use the sleep.target.  The goal is to 
have the following run before the system goes into sleep/hibernate triggered by 
whatever mechanism systemd uses to detect when the user sleeps or hibernates 
the system: '/usr/bin/psd sync'

My current user unit is linked below (psd.service).
https://github.com/graysky2/profile-sync-daemon/tree/master/init

Thank you in advance for your suggestions.
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel