Hello!
I want to run a script with systemd at shutdown.
I put the script file in /lib/systemd/system-shutdown/ and made it executable.
It won't worked...
Hello!
I want to run a script with systemd at shutdown.
I put the script file in /lib/systemd/system-shutdown/ and made it executable.
It won't worked...
I also asked for help in Arch Linux Forums and with help of MK13 user it was possible to solve this problem: https://bbs.archlinux.org/viewtopic.php?id=188858
I had to create two files.
/usr/lib/systemd/system/uptime.service:
/etc/rc.local.shutdown:Code:[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
Then execute two commands:Code:echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')" >> /home/jribeiro/registo_de_uptime
Code:sudo systemctl start uptimeMany thanks to MK13 (Registered User of Arch Linux Forums)Code:sudo systemctl enable uptime
If you just wanted to log the reboot and shutdown times, you can also get them from the 'last -x' command
The below takes that and filters to only show the reboot and shutdown lines:
last -x | grep -E 'reboot|shutdown'reboot system boot 3.10.0-123.8.1.e Thu Oct 23 22:24 - 20:12 (1+21:48)
shutdown system down 3.10.0-123.8.1.e Thu Oct 23 22:23 - 22:24 (00:01)
reboot system boot 3.10.0-123.8.1.e Thu Oct 23 00:02 - 22:23 (22:20)
shutdown system down 3.10.0-123.8.1.e Thu Oct 23 00:02 - 00:02 (00:00)
reboot system boot 3.10.0-123.8.1.e Tue Oct 21 20:58 - 00:02 (1+03:04)
shutdown system down 3.10.0-123.8.1.e Tue Oct 21 20:57 - 20:58 (00:00)
reboot system boot 3.10.0-123.8.1.e Sat Oct 18 22:33 - 20:57 (2+22:24)
shutdown system down 3.10.0-123.8.1.e Sat Oct 18 22:33 - 22:33 (00:00)
reboot system boot 3.10.0-123.8.1.e Tue Oct 14 21:59 - 22:33 (4+00:33)
My goal is to log the uptime of the machine. I'm not interested to log reboot and shutdown times. Thanks anyway.
Thank for that, pfedort, that solve for me similar problem.
You're welcome, chepioq.
I'm glad to hear that it helped you.![]()
That command
doesn't return an accurate value. When I run that on my laptop I get a result of:echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')"
But the full result from 'uptime' at the same time was:echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')"
Sun Oct 26 19:23:36 EDT 2014 - 2 days
So the first command was missing the "20:59" so that could under report your uptime by nearly a dayuptime
19:23:58 up 2 days, 20:59, 4 users, load average: 0.06, 0.04, 0.05
I'd suggest using -p "pretty mode" to avoid needing the sed stuff and then you'd get the full uptime in days, hours and minutes.
Unfortunately I think the uptime "-p" pretty print option only exists in procps-ng 3.3.9 and higher, and Fedora 20 is still on 3.3.8. I got the above to work on Centos 7 that uses 3.3.9echo "$(date) - $(/usr/bin/uptime -p)" >> /home/jribeiro/registo_de_uptime
Sun Oct 26 19:35:53 EDT 2014 - up 2 days, 21 hours, 11 minutes
Last edited by marko; 27th October 2014 at 04:16 AM.
I try rebuild the F21 package for F20, but that don't work, 3 dependencies problems...
marko,
Like you said I don't have that option on the uptime command.
In my case, it is not that bad because my computer stays powered less than a day.
Last edited by pfedort; 27th October 2014 at 05:42 PM.
chepioq,
Thanks for the information.
Last edited by pfedort; 27th October 2014 at 05:42 PM.
marko,
I agree . Maybe this command works:
Code:$ echo "$(date) - $(/usr/bin/uptime |awk -F, '{print $1,$2}' | sed 's/:/h, /g;s/^.*up */Uptime: /; s/ *[0-9]* user.*//;s/[0-9]$/&m/;s/ day. */d, /g')"
If your laptop stays powered more than a day can you try it in your laptop?
Last edited by pfedort; 28th October 2014 at 01:35 PM.
You can test your code with a text file, like test.txt with :and the command look like :Code:19:23:58 up 2 days, 20:59, 4 users, load average: 0.06, 0.04, 0.05The result :Code:$ echo "$(date) - $(cat /path/to/test.txt |awk -F, '{print $1,$2}' | sed 's/:/h, /g;s/^.*up */Uptime: /; s/ *[0-9]* user.*//;s/[0-9]$/&m/;s/ day. */d, /g')"
It's "mar." because my locale is french, for you I think it will be "tue."Code:mar. oct. 28 21:02:54 CET 2014 - Uptime: 2d, 20h, 59m
chepioq,
For me it will be "Ter" because my locale is portuguese.
Good improvisation. You showed me that the command works.
Merci.![]()
Last edited by pfedort; 28th October 2014 at 10:54 PM.