LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2016, 10:07 AM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 184

Rep: Reputation: 2
how to make init.d script hold shudown process


I have a script I need to run , lat thing at shutdown time.
But server should not go down until script is completed successfully.



so
A-script---creates--->B-scrip, B-script---doing work for me.
A-script---check/loops--->If B-script is done.

in that scenario
1-would shutdown process wait till A-script says "I am done"
2-or shutdown process would abandon A-script and go on with shutting down the server ?

Thank you.
 
Old 04-30-2016, 11:13 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Check this out:
http://unix.stackexchange.com/questi...efore-shutdown

I think you are overcomplicating matters slightly.

Best regards,
HMW
 
Old 04-30-2016, 11:38 AM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 184

Original Poster
Rep: Reputation: 2
Thanks for the link.
But I don't think it addresses the issue of reboot being held by init.d script.
 
Old 04-30-2016, 12:51 PM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by PoleStar View Post
Thanks for the link.
But I don't think it addresses the issue of reboot being held by init.d script.
I'm not sure I understand, but...
Quote:
Originally Posted by stackexchange
Runlevel 0 corresponds to shutdown, and 6 to reboot.
Quote:
Originally Posted by stackexchange
write your script, store it in /etc/init.d/, then create a symlink in each of /etc/rc0.d and /etc/rc6.d (if you want both) pointing to your script.
 
Old 04-30-2016, 01:18 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,043

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
It depends on the distribution/version i.e. if it is running SysV or systemd. If it is running systemd it would be better to create a service that calls your script(s) versus init.d. You indicate you want it to be the last thing to run prior to shutdown but that might depend on what the script is actually doing.
 
Old 04-30-2016, 04:12 PM   #6
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Are you using SysVInit or systemd? I have implemented the running of a script during shutdown/reboot which blocks the shutdown/reboot until it is finished under both SysVInit and systemd. I remember it taking barely any time at all to do it SysVInit and it worked just fine and I didn't touch it at all for about a decade. Then I spent about a year on and off swearing about trying to re-implement it for systemd. Post whether you're using SysVInit or systemd and what distro/version you're using and I'll dig out my relevant solution.
 
1 members found this post helpful.
Old 04-30-2016, 05:55 PM   #7
PoleStar
Member
 
Registered: Jul 2010
Posts: 184

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by arizonagroovejet View Post
Post whether you're using SysVInit or systemd and what distro/version you're using and I'll dig out my relevant solution.

RHEL 6, mostly, but could be RHEL5 also.
sles 11 mostly.

Thanks
 
Old 04-30-2016, 05:58 PM   #8
PoleStar
Member
 
Registered: Jul 2010
Posts: 184

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by HMW View Post
I'm not sure I understand, but...
Perhaps my post was not clear.
I want a script added in shutdown process, I want that process to make shutdown process wait till that script is done. Only then shutdown process should be allowed to continue.
 
Old 05-01-2016, 02:14 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,202

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
The commands shutdown,reboot,halt wait until all stop scripts have finished.
If a stop script does an asynchronous kill command then a sleep command should follow to give the killed process some time to properly terminate. A paranoid stop script will test with a kill -0 after a sleep, and eventally repeat it a couple of times.
(systemd, as in RHEL7 and SLES12, normally keeps track of the PIDs itself.)
 
1 members found this post helpful.
Old 05-02-2016, 08:09 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by PoleStar View Post
RHEL 6, mostly, but could be RHEL5 also.
sles 11 mostly.
So SysVInit then. I've looked up what I did on SLED 11 to get
  • A script which ran during boot after networking came up and holds up boot until it's done
  • A script which ran during shutdown/reboot before networking turned off and held up shutdown/reboot until it's done
and there's not much too it.

File /etc/init.d/foo
Code:
#!/bin/bash

#
# /etc/init.d/foo
#
### BEGIN INIT INFO
# Provides:          foo
# Required-Start:    network
# Should-Start:      $null
# Required-Stop:     xdm
# Should-Stop:	     $null
# Default-Start:     5
# Default-Stop:      5
# Short-Description: Stuff
# Description:       Stuff
### END INIT INFO

. /etc/rc.status

rc_reset


case "$1" in
   start)
      /etc/init.d/foo.d/start
   ;;
   stop)
      /etc/init.d/foo.d/stop
   ;;
   restart)
      $0 stop
      $0 start
      rc_status
      ;;
   *)
      echo "Usage $0 (start|stop|restart)"
      exit 1; 
      ;;
esac 

rc_exit
Then just run
Code:
$ insserv foo
Also in /etc/sysconfig/boot I set
Code:
RUN_PARALLEL=no
This slows down boot as it makes services start one after another rather than trying to do them as quickly as possible. I was doing it in an environment where the machine's were effectively unusable without working network and some other services, and without setting RUN_PARALLEL to no GDM would start up before services required to allow people to log in were running, which would have been extremely annoying in production.
 
Old 05-02-2016, 08:41 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,202

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Here an example for a paranoid stop/kill script.
Code:
...
getpid(){
  # pgrep options:
  # -o # the originator only (not its children)
  # -u user # owner of the program
  # -f # pathname of the program
  # -x # exact match of the program name
  #pid=`pgrep -f -u user -o progpathname`
  pid=`pgrep -x -u user -o progname`
}

...
  stop)
  if getpid
  then
    kill $pid
    for i in 1 1 2 2 3 3 4 4 5 5 6 6 7 7
    do
      sleep $i
      kill -0 $pid 2>/dev/null || exit 0 # success
    done
    exit 1 # failed
  fi
  ;;
...
Comment on the previous post:
/etc/sysconfig/boot is specific to SuSE 10+11. If you have a proper dependency header
Code:
### BEGIN INIT INFO
...
### END INIT INFO
in all start/stop scripts, there is no need for RUN_PARALLEL=no
 
Old 05-02-2016, 09:08 AM   #12
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by MadeInGermany View Post
If you have a proper dependency header
Code:
### BEGIN INIT INFO
...
### END INIT INFO
in all start/stop scripts, there is no need for RUN_PARALLEL=no
Except that in my experience there was a need for RUN_PARALLEL=no, which I described in my post. Well I guess I could have spent time messing around with the dependency headers and trying to ensure my changes weren't wiped out by package updates, but that seemed like an awful lot of effort compared to changing 'yes' to 'no' in a single file.
 
Old 05-02-2016, 09:23 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,202

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
That's true. Package updates often overwrite the start/stop scripts, backing out a custom dependency header.
With RUN_PARALLEL=no it will go to the SysV method: run the S* (start) or K* (stop) scripts (that are normally links to package files in init.d/) in alphabetical order. And package updates won't change custom S* or K* scripts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] init script successfully triggers daemon process but doesn't create pid file -Deb7 linuxecho Linux - Newbie 1 02-17-2014 10:45 AM
Writing Debian init script for process that has no syslog integration varspool Linux - Server 1 05-20-2011 03:00 AM
init script (stop) doesn't remove process IDs tdnnash25 Linux - Server 3 12-23-2009 09:01 PM
make costume init script sezam2 Linux - General 1 04-30-2009 11:17 AM
how to make ppid of a process as init without killing its parent shantha Programming 10 12-17-2004 07:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:15 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration