You are not logged in.

#1 2014-10-24 12:08:00

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

[SOLVED] Run script on systemd shutdown and reboot

Hello!

I am trying to log uptime on shutdown and reboot with no success.

I create the file /usr/lib/systemd/system/uptime.service:

 [Unit]
Description=/etc/rc.local.shutdown Compatibility
ConditionFileIsExecutable=/etc/rc.local.shutdown
DefaultDependencies=no
After=rc-local.service basic.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/etc/rc.local.shutdown
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target 

/etc/rc.local.shutdown:

 [jribeiro@arkosta ~]$ ls -l /etc/rc.local.shutdown 
-rwxr-xr-x 1 root root 107 Out 24 12:42 /etc/rc.local.shutdown
[jribeiro@arkosta ~]$ cat  /etc/rc.local.shutdown 
echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')"  >> /home/jribeiro/registo_de_uptime

Any ideas?

Last edited by joseribeiro (2014-10-25 19:57:59)

Offline

#2 2014-10-24 12:49:50

teateawhy
Member
From: GER
Registered: 2012-03-05
Posts: 1,138
Website

Re: [SOLVED] Run script on systemd shutdown and reboot

May i suggest using the package community/uptimed ?

Offline

#3 2014-10-24 13:08:15

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

Thanks but I want to know what i'm doing wrong.

Offline

#4 2014-10-24 15:47:07

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: [SOLVED] Run script on systemd shutdown and reboot

Have you enabled your service ?

It may be better to keep your own services under /etc/systemd/system instead of /usr/lib/...

Maybe a dependency issue e.g.

After=rc-local.service basic.target

?


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#5 2014-10-24 17:26:03

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

Yes, the service is enabled.

Offline

#6 2014-10-24 18:52:02

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

Is the service called at all?

What does

journalctl -u uptime

say?

Also, try to strip everything from your unit file that is not absolutely necessary for your service to work. You might try:

[Unit]
Description=/etc/rc.local.shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/etc/rc.local.shutdown

[Install]
WantedBy=shutdown.target 

Further, try a simple script that only creates a file in your home folder as target.

EDIT: What solved the issue on my machine: Add

#!/bin/bash

to the script. See [SOLVED]Starup sript not getting executed in systemd

Last edited by MK13 (2014-10-24 19:36:32)

Offline

#7 2014-10-24 21:16:44

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

journalctl -u uptime says:

[jribeiro@arkosta ~]$ journalctl -u uptime
-- Logs begin at Sex 2014-10-24 22:02:15 WEST, end at Sex 2014-10-24 22:10:39 WEST. --

sudo journalctl -u uptime says:

[jribeiro@arkosta ~]$ sudo journalctl -u uptime
-- Logs begin at Sex 2014-10-24 22:02:03 WEST, end at Sex 2014-10-24 22:12:07 WEST. --

/usr/lib/systemd/system/uptime.service:

[jribeiro@arkosta ~]$ sudo cat  /usr/lib/systemd/system/uptime.service 
[Unit]
Description=/etc/rc.local.shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/etc/rc.local.shutdown

[Install]
WantedBy=shutdown.target

Still no success...

Last edited by joseribeiro (2014-10-24 21:17:30)

Offline

#8 2014-10-24 21:37:51

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

Did you add the shebang in the rc.local.shutdown script?

Could you please

systemctl start uptime
systemctl -l status uptime

Offline

#9 2014-10-24 21:41:30

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

Sure I can:

[jribeiro@arkosta ~]$ sudo systemctl -l status uptime
● uptime.service - /etc/rc.local.shutdown Compatibility
   Loaded: loaded (/usr/lib/systemd/system/uptime.service; enabled)
   Active: inactive (dead) since Sex 2014-10-24 22:39:28 WEST; 10s ago
  Process: 6220 ExecStart=/etc/rc.local.shutdown (code=exited, status=0/SUCCESS)
 Main PID: 6220 (code=exited, status=0/SUCCESS)

Offline

#10 2014-10-24 22:12:13

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

Dumb question: Did you check for the 'registo_de_uptime' file in your home folder after starting the service manually?

On my machine, with the stripped unit file and the script with the shebang, everything works fine. Lets double check that before moving on:

Replace the content of the rc.local.shutdown script with

#!/bin/bash
echo "$(date)" >> /home/jribeiro/uptime

and try to start the service again via

systemctl start uptime

and check whether a file called 'uptime' is present in your home folder.

The status query of the uptime service shows that the service run without errors so it should work...

Last edited by MK13 (2014-10-24 22:14:44)

Offline

#11 2014-10-25 12:45:26

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

Before starting the service the 'registo_de_uptime' file was empty.

After starting the service the 'registo_de_uptime' is modified:

[jribeiro@arkosta ~]$ cat  /home/jribeiro/registo_de_uptime                                                   
Sáb Out 25 13:36:29 WEST 2014 - 4 min 

Then I rebooted my system and I checked the 'registo_de_uptime' file:

[jribeiro@arkosta ~]$ cat  /home/jribeiro/registo_de_uptime 
Sáb Out 25 13:36:29 WEST 2014 - 4 min

No changes...

P.S.: I tried same thing with "echo "$(date)" >> /home/jribeiro/uptime" and the result is equal. The file changes manually but don't changes automatically.

Last edited by joseribeiro (2014-10-25 12:55:37)

Offline

#12 2014-10-25 13:12:26

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

Could you post output of

sudo journalctl -b -0 -u uptime

Try altering your unit file to

[Unit]
Description=/etc/rc.local.shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/rc.local.shutdown
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target

Then starting the service manually and shutdown afterwards.

Offline

#13 2014-10-25 19:04:40

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

I altered the uptime unit file:

[jribeiro@arkosta ~]$ cat /usr/lib/systemd/system/uptime.service 
[Unit]
Description=/etc/rc.local.shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/rc.local.shutdown
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target

Then I started the uptime service manually:

[jribeiro@arkosta ~]$ sudo systemctl start uptime 

I checked the 'registo_de_uptime' file:

[jribeiro@arkosta ~]$ cat /home/jribeiro/registo_de_uptime 
Sáb Out 25 19:43:31 WEST 2014 - 2 min

After that I rebooted my system.
I checked the 'registo_de_uptime' file, again:

[jribeiro@arkosta ~]$ cat /home/jribeiro/registo_de_uptime 
Sáb Out 25 19:43:31 WEST 2014 - 2 min
Sáb Out 25 19:44:27 WEST 2014 - 3 min

Everything seems to be alright. I wanted to make the ultimate test.
With that in mind I rebooted the system.
I checked the 'registo_de_uptime' file, once more:

[jribeiro@arkosta ~]$ cat /home/jribeiro/registo_de_uptime 
Sáb Out 25 19:43:31 WEST 2014 - 2 min
Sáb Out 25 19:44:27 WEST 2014 - 3 min

No success...
It works when I start the service manually and in the right next reboot, after that it won't work.


Here is the journal's ouput:

[jribeiro@arkosta ~]$ sudo journalctl -b -0 -u uptime 
-- Logs begin at Sex 2014-10-24 22:02:03 WEST, end at Sáb 2014-10-25 20:05:29 WEST. --

By the way:

[jribeiro@arkosta ~]$ sudo systemctl status uptime
● uptime.service - /etc/rc.local.shutdown Compatibility
   Loaded: loaded (/usr/lib/systemd/system/uptime.service; enabled)
   Active: inactive (dead)

Last edited by joseribeiro (2014-10-25 19:07:32)

Offline

#14 2014-10-25 19:34:47

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

Could you post output of

systemctl status uptime

after starting the service manually and

journalctl -b -u uptime

Last edited by MK13 (2014-10-25 19:38:35)

Offline

#15 2014-10-25 19:41:42

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

I don't give up.
So, I altered the Install section and I changed 'shutdown.target' for 'multi-user.target'.

Here it is the uptime.service's final version:

[Unit]
Description=/etc/rc.local.shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/rc.local.shutdown
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Now, it works! big_smile

Many thanks to MK13 wink

Last edited by joseribeiro (2014-10-25 19:43:43)

Offline

#16 2014-10-25 19:44:25

MK13
Member
From: Germany
Registered: 2014-04-12
Posts: 80

Re: [SOLVED] Run script on systemd shutdown and reboot

You're welcome, glad to hear it's working now smile

Don't forget to mark the topic as solved.

Last edited by MK13 (2014-10-25 19:48:00)

Offline

#17 2014-10-25 19:54:13

joseribeiro
Member
Registered: 2013-02-17
Posts: 73

Re: [SOLVED] Run script on systemd shutdown and reboot

I will do that.

Thanks again!!! wink

Last edited by joseribeiro (2014-10-25 19:56:45)

Offline

Board footer

Powered by FluxBB