[SOLVED] how to run a command when reboot & shutdown

Questions about Grub, UEFI,the liveCD and the installer
Forum rules
Before you post please read how to get help
Post Reply
reptilezone2002
Level 2
Level 2
Posts: 53
Joined: Thu Aug 02, 2012 2:27 am

[SOLVED] how to run a command when reboot & shutdown

Post by reptilezone2002 » Sat Sep 15, 2018 10:07 am

hi
hope this is the right place to post this question

i need to kill the process motion every time i reboot & shutdown the pc
can i use the command

sudo killall -9 motion

& how can i run this command when i reboot & shutdown the pc
why i need to do this is everytime i reboot the motion precess takes 5 mins to end
im starting motion with this command sudo motion manually
im running mint 19
Last edited by reptilezone2002 on Wed Sep 19, 2018 9:16 am, edited 1 time in total.

lazarus
Level 3
Level 3
Posts: 148
Joined: Mon Jul 02, 2018 11:36 pm
Location: Oberon, OZ

Re: how to run a command when reboot & shutdown

Post by lazarus » Sun Sep 16, 2018 10:06 pm

Ideally you'd write a script that'll both start & shutdown the process, and place that in /etc/init.d, with a symlink to it in /etc/rcS.d to start the process and more symlinks to it in the /etc/rc[0-6].d folders to terminate the process at shutdown. This involves some... complexity.

(An irrelevant aside: /etc/rc.local would an even better choice for an arbitrary script, but I'm unsure whether Linux Mint actively supports it? Anyone?)

So, instead of that, I'd suggest a compromise.

To get it auto-starting on boot:

As it needs root authorisation - I assume it does since you're calling it via sudo - I'd write a quick calling script and save it as /etc/cron.d/motion-boot

Code: Select all

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
@reboot root sleep 10s && motion

...should be approximately right. Make sure it has the same owner (root) and permissions as everything else in that dir. ;)

(If the cmd did NOT need root authorisation AND you only wanted it on a per User basis, then I'd log in as that user and simply add it via the Startup Applications utility in the LM Menu)

Auto-stopping at shutdown will involve the aforementioned /etc/rc*.d runlevel dirs:

Write yourself a simple script that performs the shutdown, saving it to /etc/init.d if you wish, although personally I'd save it elsewhere 'cos it's not exactly "up-to-standard" of the rest of the scripts residing in there. ;)

I'd use something akin to the f9ollowing. (This is ugly. There should be a better way. Hmmmm....)

Code: Select all

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
killall -9 motion
Then make symlinks from it to /etc/rc0.d and to /etc/rc6.d (rc0.d contains the scripts that run on clean shutdown & rc6.d contains those run for shutdown & reboot)

Ensure they have correct permissions, ownership by root, etc. (ie. the same as every other script in those dirs!)

You need to be very careful when choosing a name, the scripts in these runlevel dirs are executed in ASCII order, so choose a name that ensures it is run at the right time in the sequence.

BTW, don't take the above as gospel; it's all off the top of my head and may easily contain misinformation. Take it as a signpost indicating the drirection you need to go... ;)
- Andy

I may be weird but I'm saving up to become eccentric.

reptilezone2002
Level 2
Level 2
Posts: 53
Joined: Thu Aug 02, 2012 2:27 am

Re: how to run a command when reboot & shutdown

Post by reptilezone2002 » Tue Sep 18, 2018 9:55 am

lazarus wrote:
Sun Sep 16, 2018 10:06 pm
BTW, don't take the above as gospel; it's all off the top of my head and may easily contain misinformation. Take it as a signpost indicating the drirection you need to go... ;)
it didnt work

i created the files in those directories rc0.d & rc6.d not symlinks with permissions to root & executable & saved with the filename K01motion but the issue is still there heres a screenshot

IMG_20180918_190226.jpg
IMG_20180918_190225.jpg

gm10
Level 12
Level 12
Posts: 4078
Joined: Thu Jun 21, 2018 5:11 pm

Re: how to run a command when reboot & shutdown

Post by gm10 » Tue Sep 18, 2018 10:03 am

reptilezone2002 wrote:
Tue Sep 18, 2018 9:55 am
it didnt work

i created the files in those directories rc0.d & rc6.d not symlinks with permissions to root & executable & saved with the filename K01motion but the issue is still there heres a screenshot


IMG_20180918_190226.jpg
IMG_20180918_190225.jpg
I'll stay out of the proposed solution above but other ways to deal with this are to either reduce the default timeouts in /etc/systemd/system.conf or, if you only want to handle specific problematic processes, you can create a session cleanup script:

Code: Select all

#!/bin/sh
printf "[SeatDefaults]\nsession-cleanup-script=/etc/lightdm/session-cleanup.sh\n" | sudo tee /etc/lightdm/lightdm.conf.d/10-session-cleanup.conf
followed by

Code: Select all

printf "#!/bin/sh\n# add session cleanup commands here, e.g.:\nkillall motion\n" | sudo tee /etc/lightdm/session-cleanup.sh
sudo chmod +x /etc/lightdm/session-cleanup.sh
you can then further edit /etc/lightdm/session-cleanup.sh, if necessary. This script runs on logout rather than on shutdown, but your problem seems to be with a stuck user session.

User avatar
thx-1138
Level 6
Level 6
Posts: 1235
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: how to run a command when reboot & shutdown

Post by thx-1138 » Tue Sep 18, 2018 12:35 pm

...i'd try:
sudo touch /etc/systemd/system/motionkill.service

xed admin:///etc/systemd/system/motionkill.service

Code: Select all

[Unit]
Description=Kill motion
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/usr/bin/killall -9 motion

[Install]
WantedBy=halt.target reboot.target shutdown.target
sudo systemctl enable motionkill

User avatar
Flemur
Level 16
Level 16
Posts: 6180
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: how to run a command when reboot & shutdown

Post by Flemur » Tue Sep 18, 2018 1:48 pm

reptilezone2002 wrote:
Sat Sep 15, 2018 10:07 am
i need to kill the process motion every time i reboot & shutdown the pc
can i use the command
sudo killall -9 motion
im starting motion with this command sudo motion manually
Is "motion" the "monitors the video signal from one or more cameras and ... it can detect motion." ??

If so, why are you starting it w/sudo?
Edit: that's likely the reason why it hangs...does it not work w/o 'sudo'?

As you can see from the other answers, the implementation of the simple idea of running a script when you shutdown is complicated and kludgey; I use local scripts to shutdown and reboot, e.g.
Shutdown might look like:

Code: Select all

killall -9 motion
/bin/systemctl poweroff -i
Note no 'sudo' on the killall, which you'd probably need (or use gksudo, which is missing from Mint 19 but you can replace it) if you started it w/sudo.

Then call "Shutdown" from your menu.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

reptilezone2002
Level 2
Level 2
Posts: 53
Joined: Thu Aug 02, 2012 2:27 am

Re: how to run a command when reboot & shutdown

Post by reptilezone2002 » Wed Sep 19, 2018 9:15 am

thx-1138 wrote:
Tue Sep 18, 2018 12:35 pm
...i'd try:
sudo touch /etc/systemd/system/motionkill.service

xed admin:///etc/systemd/system/motionkill.service

Code: Select all

[Unit]
Description=Kill motion
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/usr/bin/killall -9 motion

[Install]
WantedBy=halt.target reboot.target shutdown.target
sudo systemctl enable motionkill
this worked... tx very much

reptilezone2002
Level 2
Level 2
Posts: 53
Joined: Thu Aug 02, 2012 2:27 am

Re: how to run a command when reboot & shutdown

Post by reptilezone2002 » Wed Sep 19, 2018 9:16 am

Flemur wrote:
Tue Sep 18, 2018 1:48 pm
reptilezone2002 wrote:
Sat Sep 15, 2018 10:07 am
i need to kill the process motion every time i reboot & shutdown the pc
can i use the command
sudo killall -9 motion
im starting motion with this command sudo motion manually
Is "motion" the "monitors the video signal from one or more cameras and ... it can detect motion." ??

If so, why are you starting it w/sudo?
Edit: that's likely the reason why it hangs...does it not work w/o 'sudo'?

As you can see from the other answers, the implementation of the simple idea of running a script when you shutdown is complicated and kludgey; I use local scripts to shutdown and reboot, e.g.
Shutdown might look like:

Code: Select all

killall -9 motion
/bin/systemctl poweroff -i
Note no 'sudo' on the killall, which you'd probably need (or use gksudo, which is missing from Mint 19 but you can replace it) if you started it w/sudo.

Then call "Shutdown" from your menu.
im getting permssion error on creating /var/run/motion folder

Post Reply

Return to “Installation & Boot”