Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What do I need to put in the [install] section, so that systemd runs /home/me/so.pl right before shutdown and also before /proc/self/net/dev gets destroyed?

[Unit]
Description=Log Traffic

[Service]
ExecStart=/home/me/so.pl

[Install]
?
share|improve this question

3 Answers 3

The suggested solution is to run the service unit as a normal service - have a look at the [Install] section. So everything has to be thought reverse, dependencies too. Because the shutdown order is the reverse startup order. That's why the script has to be placed in ExecStop.

The following solution is working for me:

[Unit]
Description=...

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=<your script/program>

[Install]
WantedBy=multi-user.target

Have a look at RemainAfterExit this line is needed because otherwise the service get's stopped as soon as the start action finishes.

I just got it from systemd IRC, credits are going to mezcalero.

share|improve this answer
up vote 1 down vote accepted

As far as I can see this does what I need (but I don't exactly why).

[Unit]
Description=Log Traffic
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target


[Service]
ExecStart=/usr/local/bin/perl /home/me/log_traffic.pl --stop
Type=oneshot
share|improve this answer

I am not totally sure but i don't think you need the install part though i added it explicitly. I also didn't test it but i think it should help you get started:

[Unit]
Description=Log Traffic
Requires=network.target
After=network.target
Before=shutdown.target
DefaultDependencies=no

[Service]
ExecStart=/home/me/so.pl
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
share|improve this answer
    
When I try this the script gets executed soon after the start (boot). –  sid_com May 24 '12 at 8:05
    
@sid_com After reading thread.gmane.org/gmane.comp.sysutils.systemd.devel/4515/… try to add DefaultDependencies=no and maybe remove the install section. Otherwise it may help to remote the after/requires lines. –  Ulrich Dangel May 24 '12 at 11:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.