I’d like to run a script before my system suspends. Suspend works pretty great on my system and I set up a systemd unit to run a script on suspend: /etc/systemd/system/suspend@.service contains:
[Unit]
Description=Resume actions for user %I
Before=suspend.target
[Service]
User=%i
Type=simple
ExecStart=/home/jt/bin/nopath/presuspend.sh
[Install]
WantedBy=suspend.target
From my debugging, I can tell that the script is being run when I suspend the system. The problem is that it is not run before the system suspends. This means that either half the script runs before suspending and the other half after resuming, or the script runs after my network goes down so I can’t move files to a network share.
During this test my system didn’t even actually suspend, I just saw the screen lock and then the network went down. I see that the script really did run since I had it write a timestamp.
I think you’re onto something! The script is failing to start from systemd every time:
Sep 09 12:27:06 mybox systemd[1]: Failed to start suspend actions for user service.
░░ Subject: A start job for unit suspend@service.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit suspend@service.service has finished with a failure.
░░
░░ The job identifier is 2960 and the job result is failed.
This log doesn’t really tell me much. I moved it out of my home anyway. Do you think it’s failing because
Running the script itself seems fine:
$ bash -x /usr/local/bin/mvtorrents.sh
+ date
+ mv -v /home/jt/Downloads/fake.torrent /media/krondor/incoming/
+ date
+ touch /media/krondor/incoming/mvtorrents-ran
+ exit 0
Do you think systemd is not running the script as my user so it runs into permission problems or something? Thanks again for your help!
I do. I am not super familiar with using a User=%i line, but I have been reading a little bit and from what I understand it looks like your service is running as the user service:
I think when you start an instance of this service, you provide a value for %i as an argument after the @ and before the .service. (source)
systemctl start suspend@john.service
Here, %i is replaced with “john,” and the service will run with the user account “john”.
I believe I was mistaken about this; if you are running it as your user it should be fine for it to be in /home/user.
This makes sense, now that I think about it the script must be running as me because it makes other files as me as part of debugging, and that was running before. So what else should I try other than oneshot?
So running sudo systemctl start suspend@jt.service does seem run the script properly. It makes my debugging files, it moves the files out of my downloads properly. I guess the problem is really getting it to run before suspending still.
When I actually suspend my system it doesn’t work right. It runs as the network is going down so it either crashes while a move is in process or fails because the server is unreachable. In the past I observed it running half the script before suspend and half after. I just did a suspend to tell you exactly what happened and my machine didn’t wake back up so I had no evidence of it running at all. A test file or debugging file didn’t make it to my server.
I’m starting to think that even if I get this working properly, it’s still going to suck if my machine just doesn’t wake up from suspend sometimes. It’ll mean lost data on the ramdisk which means I can’t rely on this anyway. Maybe I should just stop trying
Do you have these issues with suspending outside of the new systemd service? In other words, if you disable this new service altogether, does your machine ever have issues not waking properly? It seems like a separate issue.
It might be actually. It might only be freezing when there’s actually something to move. The main issue is still that I need this script to run and complete before the other suspend stuff happens.