Why the stop-log service run both at start and shutdown?

I want to make start-log service run automatically in os's starting time.

    sudo vim /etc/systemd/system/start-log.service
    [Unit]
    Description=start log

    [Service]
    User=root
    ExecStart=/bin/bash  /home/debian9/start.sh 
    ExecStop=/bin/bash   /home/debian9/stop.sh
    RemainAfterExit=yes

    [Install]
    WantedBy=multi-user.target

    cat /home/debian9/start.sh
    /bin/echo "start" >> /home/debian9/log.info
    /bin/date >>  /home/debian9/log.info

    cat /home/debian9/stop.sh
    /bin/echo "stop" >> /home/debian9/log.info
    /bin/date >>  /home/debian9/log.info

Enable start-log service and reboot pc.

    cat  log.info
    start
    Fri Apr 13 21:47:08 HKT 2018

Notice that no info such as stop Fri Apr 13 21:xxxxx HKT 2018 in log.info.

It means that ExecStop=/bin/bash /home/debian9/stop.sh in start-log service can't execute at start time.

Now add a stop-log service ,i want to record some info automatically at my pc's shutdown time.

    sudo vim /etc/systemd/system/stop-log.service    
    [Unit]
    Description=stop log
    Before=shutdown.target  

    [Service]
    User=root
    ExecStop=/bin/bash  /home/debian9/stop_shutdown.sh 
    ExecStart=/bin/bash  /home/debian9/start_shutdown.sh 
    RemainAfterExit=yes

    [Install]
    WantedBy=multi-user.target

Content in stopshutdown.sh and startshutdown.sh

    cat /home/debian9/stop_shutdown.sh
    /bin/echo  "stop in shutdown" >> /home/debian9/log.info
    /bin/date  >> /home/debian9/log.info
    cat /home/debian9/start_shutdown.sh
    /bin/echo "start in shutdown" >>/home/debian9/log.info
    /bin/date  >> /home/debian9/log.info

Enable stop-log service and reboot pc.

    cat log.info
    stop in shutdown
    Fri Apr 13 21:58:21 HKT 2018
    start
    start in shutdown
    Fri Apr 13 22:00:20 HKT 2018
    Fri Apr 13 22:00:20 HKT 2018

start in shutdown appear in log.info ,it means that ExecStart=/bin/bash /home/debian9/start_shutdown.sh in stop-log service executed ; and the time is the same as start (in start-log service),it means that ExecStart=/bin/bash /home/debian9/start_shutdown.sh in stop-log service executed at pc'start time .

Why ExecStart in stop-log service run at the my pc's start time?

1 Reply

Hi, have you tried enabling it to run with systemctl or update-rc.d?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct