FedoraForum.org - Fedora Support Forums and Community
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run script with systemd at shutdown

    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...

  2. #2
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    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:

    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
    /etc/rc.local.shutdown:
    Code:
    echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')"  >> /home/jribeiro/registo_de_uptime
    Then execute two commands:

    Code:
    sudo systemctl start uptime
    Code:
    sudo systemctl enable uptime
    Many thanks to MK13 (Registered User of Arch Linux Forums)

  3. #3
    Join Date
    Jun 2004
    Location
    Maryland, US
    Posts
    15,399
    Mentioned
    186 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    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)

  4. #4
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    My goal is to log the uptime of the machine. I'm not interested to log reboot and shutdown times. Thanks anyway.

  5. #5
    Join Date
    Jun 2007
    Location
    Aubagne France
    Age
    71
    Posts
    675
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    Thank for that, pfedort, that solve for me similar problem.

  6. #6
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    You're welcome, chepioq.
    I'm glad to hear that it helped you.

  7. #7
    Join Date
    Jun 2004
    Location
    Maryland, US
    Posts
    15,399
    Mentioned
    186 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    That command
    echo "$(date) - $(/usr/bin/uptime | sed 's/.*up \([^,]*\), .*/\1/')"
    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/')"
    Sun Oct 26 19:23:36 EDT 2014 - 2 days
    But the full result from 'uptime' at the same time was:
    uptime
    19:23:58 up 2 days, 20:59, 4 users, load average: 0.06, 0.04, 0.05
    So the first command was missing the "20:59" so that could under report your uptime by nearly a day

    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.
    echo "$(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
    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.9
    Last edited by marko; 27th October 2014 at 04:16 AM.

  8. #8
    Join Date
    Jun 2007
    Location
    Aubagne France
    Age
    71
    Posts
    675
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    Quote Originally Posted by marko
    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.9
    It's true, your command don't work with F 20 and procps-ng 3.3.8. (the message is "-p is not an option for uptime")

    May be you can rebuild the 3.3.9 version of F21 to F20.

  9. #9
    Join Date
    Jun 2007
    Location
    Aubagne France
    Age
    71
    Posts
    675
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    I try rebuild the F21 package for F20, but that don't work, 3 dependencies problems...

  10. #10
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    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.

  11. #11
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    chepioq,

    Thanks for the information.
    Last edited by pfedort; 27th October 2014 at 05:42 PM.

  12. #12
    Join Date
    Jun 2004
    Location
    Maryland, US
    Posts
    15,399
    Mentioned
    186 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    Quote Originally Posted by pfedort
    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.
    Also I suspect the 'sed' command can be modified to also scrape out the hours and minutes too
    so you can get the accurate uptime.

  13. #13
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    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.

  14. #14
    Join Date
    Jun 2007
    Location
    Aubagne France
    Age
    71
    Posts
    675
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    You can test your code with a text file, like test.txt with :
    Code:
    19:23:58 up 2 days, 20:59, 4 users, load average: 0.06, 0.04, 0.05
    and the command look like :
    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')"
    The result :
    Code:
    mar. oct. 28 21:02:54 CET 2014 - Uptime: 2d, 20h, 59m
    It's "mar." because my locale is french, for you I think it will be "tue."

  15. #15
    Join Date
    Feb 2010
    Location
    Costa de Caparica, Portugal
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Run script with systemd at shutdown

    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.

Similar Threads

  1. Replies: 1
    Last Post: 16th March 2014, 12:13 AM
  2. systemd script does not run on reboot
    By rholme in forum Servers & Networking
    Replies: 14
    Last Post: 21st December 2013, 05:24 PM
  3. F18 + systemd...how do I run an init script?
    By DupermanDave in forum Using Fedora
    Replies: 8
    Last Post: 30th January 2013, 07:48 PM
  4. How to shutdown when systemd crashes?
    By clivem in forum F15 Development
    Replies: 4
    Last Post: 4th May 2011, 10:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •