WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] run a own shell at shutdown/restart how ?  (Read 5163 times)

Offline halma

  • Full Member
  • ***
  • Posts: 164
[Solved] run a own shell at shutdown/restart how ?
« on: September 18, 2020, 09:18:24 AM »
Hey all

id like to know what is the usual way under tinycorelinux 11.1 to run a shell script at shutdown or restart?
/opt/shutdown.sh seems not working...

/etc/init.d/myscript
Code: [Select]
#!/bin/busybox ash
# /etc/init.d/myscipt
#

./etc/init.d/tc-functions
useBusybox


#
#
# sichert log Dateien aus /var/log
echo "log dateien wernden gesichert..."

zielverzeichnis="/mnt/xvda/logs"
datum="$(date +"%Y-%m-%d_%H_%M")"

cd /var/log
for datei in $(ls -1); do
    tar czf "$zielverzeichnis/${datei}_${datum}.tar.gz" "$datei"
done


is simlinked from the /home/tc/myscript.sh to /etc/init.d/myscript and a modified inittab seems also not working
Code: [Select]
cat /etc/inittab
# /etc/inittab: init configuration for busybox init.
# Boot-time system configuration/initialization script.
#
::sysinit:/etc/init.d/rcS

# /sbin/getty respawn shell invocations for selected ttys.
tty1::respawn:/sbin/getty 38400 tty1
#tty2::respawn:/sbin/getty 38400 tty2
#tty3::respawn:/sbin/getty 38400 tty3
#tty4::askfirst:/sbin/getty 38400 tty4
#tty5::askfirst:/sbin/getty 38400 tty5
#tty6::askfirst:/sbin/getty 38400 tty6

# Stuff to do when restarting the init
# process, or before rebooting.
::restart:/etc/init.d/myscript
::restart:/etc/init.d/rc.shutdown
::restart:/sbin/init
::ctrlaltdel:/sbin/reboot
::shutdown:/etc/init.d/myscript
::shutdown:/etc/init.d/rc.shutdown

hvc0::respawn:/sbin/getty 38400 hvc0

How can i run a script at shutdown/restart ??
Thanks a lot and Best Regards
« Last Edit: September 21, 2020, 04:40:55 PM by Rich »
1 + 2 = 6  cause  10 - 6 = 78 ;-) lol

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11701
Re: run a own shell at shutdown/restart how ?
« Reply #1 on: September 18, 2020, 09:40:30 AM »
Hi halma
If you shut down using  poweroff  or restart using  reboot,  no scripts will be run. Those are busybox commands.
If you run:
Code: [Select]
exitcheck.sh rebootor
Code: [Select]
exitcheck.sh shutdownit will call  /opt/shutdown.sh.

If you use the  Exit  icon in the  GUI,  it too will call  exitcheck.sh.

If you are trying to backup  inittab  and the  symlink  in /etc/init.d, it won't work. The restore happens way too late.

Offline halma

  • Full Member
  • ***
  • Posts: 164
Re: run a own shell at shutdown/restart how ?
« Reply #2 on: September 20, 2020, 02:54:32 AM »
Hi Rich  :)

the exitcheck.sh command is a nice, let me call it: "workaround". Thats not the solution i want to have because on mostly every linux or bsd system the way to restart or shutdown from a user is "sudo reboot/shutdown". So mostly as a normal behavior or how I learned it and how to act accordingly from the subconscious and excuse me if I say so but I don't want to learn and memorize a new command that only works for a single system like microsoft stuff.I would like to use the command that is usually standard under a linux system: sudo reboot/shutdown

I have some Questions about the reboot/shutdown behavior of tinycorelinux. The "rc.shutdown" is the only command/script which runs at reboot/shutdown. Is this "rc.shutdown" name hardcoded?
Why is myscript not executed and also other scripts before the "rc.shutdown" starts after i have send a reboot/shutdown command to the system?
I think i have correctly inittab configured seen in the first post of this thread.

Thanks a lot
1 + 2 = 6  cause  10 - 6 = 78 ;-) lol

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11049
Re: run a own shell at shutdown/restart how ?
« Reply #3 on: September 20, 2020, 03:10:48 AM »
inittab is read so early in boot, to make changes in it read you need to remaster those in, instead of using backup.

edit: alternatively, sending SIGHUP to init after changing the file may work.
The only barriers that can stop you are the ones you create yourself.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11701
Re: run a own shell at shutdown/restart how ?
« Reply #4 on: September 20, 2020, 08:43:18 AM »
Hi halma
Maybe this will work. Edit  /etc/init.d/rc.shutdown  and have it call your script like this:
Code: [Select]
#!/bin/sh
# /etc/init.d/rc.shutdown - used by /etc/inittab to shutdown the system.
#
. /etc/init.d/tc-functions
useBusybox

/etc/init.d/myscript

clear
# Sync all filesystems.
echo "${BLUE}Syncing all filesystems."
sync; sync

----- Snip -----

Add  etc/init.d/rc.shutdown  to your backup.

Offline halma

  • Full Member
  • ***
  • Posts: 164
Re: run a own shell at shutdown/restart how ?
« Reply #5 on: September 21, 2020, 04:17:55 PM »
Hello Rich,

i had the same idea before but i dont like "hackarounds" cause its not the usually/official way, but this seems at the moemnt the only way to run a script at reboot/shutdown. Append this lines to the bootlocal.sh is doing the job

Code: [Select]
# insert personal shellscript into /etc/init.d/shutdown.rc
echo "appending personal reboot/shutdown shellscript to /etc/init.d/rc.shutdown"
shellscript_to_run="/my/script"
if ! grep "$shellscript_to_run" /etc/init.d/rc.shutdown >/dev/null; then
    sed -i '/useBusybox/a\
\
# run a script at reboot/shutdown\
'"${shellscript_to_run}"'' /etc/init.d/rc.shutdown
fi

Thanks a lot

*i cannot mark the thread a solved*
« Last Edit: September 21, 2020, 04:19:47 PM by halma »
1 + 2 = 6  cause  10 - 6 = 78 ;-) lol

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11701
Re: [Solved] run a own shell at shutdown/restart how ?
« Reply #6 on: September 21, 2020, 04:41:36 PM »
Hi halma
Adding a command to modify  /etc/init.d/shutdown.rc  from  /opt/bootlocal.sh  is another way of doing it. This should work
quite well if you update your sytem. Kudos on your  sed  skills.

... *i cannot mark the thread a solved*
That's what I'm here for. :)