LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 12-22-2016, 06:43 AM   #1
platypo
Member
 
Registered: Sep 2015
Distribution: Debian, Archlinux
Posts: 140

Rep: Reputation: Disabled
Systemd shutdown script


Hello!
I want to remotely shut down my Raspberry Pi over lan when i power off my computer. Authentication is not an issue. I've tried serveral entries and searched across the web but i'm out of ideas.

It works fine when i start this service manually:

Code:
root@pc:/lib/systemd/system# cat rpishutdown.service 
[Unit]
Description=Shutdown Raspberry Pi

[Service]
ExecStart=/usr/bin/ssh <IP> shutdown 0
but i haven't found out what dependencies it needs to be enabled in order to be executed before the network is shut down.

Further criteria that the service would need to meet:

*) it should only be executed at shutdown and not at reboot.

*) if possible it should be executed early in the shutdown-process.
Scince the RaspberryPi is powered via the PCs usb it's power supply
could be cut off before it has completely shut down. Is there a way to
make the system idle a few seconds before halt to achieve this?

Thank you.

Last edited by platypo; 12-22-2016 at 06:49 AM.
 
Old 12-22-2016, 07:02 AM   #2
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,487

Rep: Reputation: Disabled
Take a look at man shutdown.
 
Old 12-22-2016, 07:36 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Not tested...
Code:
[Unit]
Description=Pi Shutdown 
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/usr/bin/ssh <IP> shutdown 0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
This should be the first service run at shutdown. Not sure how to add a delay but should be doable if necessary.
 
Old 12-22-2016, 07:57 AM   #4
platypo
Member
 
Registered: Sep 2015
Distribution: Debian, Archlinux
Posts: 140

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Not tested...
Code:
[Unit]
Description=Pi Shutdown 
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/usr/bin/ssh <IP> shutdown 0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
This should be the first service run at shutdown. Not sure how to add a delay but should be doable if necessary.
Thanks. Doesn't work, i've tried this except the ExecStart=/bin/true. It does work when i manually stop the service though.

Quote:
Originally Posted by fatmac View Post
Take a look at man shutdown.
yes but it should ideally work both over gui as well as cli.

Last edited by platypo; 12-22-2016 at 08:02 AM.
 
Old 12-22-2016, 08:21 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Did you enable the service?
 
Old 12-22-2016, 09:14 AM   #6
platypo
Member
 
Registered: Sep 2015
Distribution: Debian, Archlinux
Posts: 140

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Did you enable the service?
Yes i did. I had to restart a few times and now it seems to be working partially. I have the following results (using debian8/kde plasma):

*) it works when i shutdown/reboot over cli
*) it doesn't work when i use the poweroff options in the kde application launcher
*) the timespan for the Raspberry Pi to be shut down seems to be right so no idle time necessary.

i was just wondering if a different approach makes sense: instead of running a unit on boot is it possible to write one that starts exactly at shutdown (and thus not on reboot)?

Thank you again.

Last edited by platypo; 12-22-2016 at 09:15 AM.
 
Old 12-22-2016, 09:44 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Not sure why it would only work via the command line. I would not expect different behavior but you could try these changes. Shutdown scripts do not run at boot.

Code:
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=ExecStop=/usr/bin/ssh <IP> shutdown 0
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Used to shutdown/reboot from CLI but since systemd replaced init "shutdown" cmnd broke? Beagleburt Debian 10 09-24-2019 07:49 PM
Systemd shutdown Phiebie Debian 2 11-07-2016 11:55 AM
Can't access full journalctl from script via systemd service even though user is in systemd-journal group iwtbf Linux - Newbie 0 02-19-2016 02:44 PM
[SOLVED] shutdown works, but /lib/systemd/system/shutdown.target does not. kaz2100 Debian 3 08-06-2014 03:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 07:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration