LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-02-2012, 09:24 AM   #1
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Rep: Reputation: Disabled
910912 - how can i know that linux is shutting down?


i've a perl script in which i need to know whether the system is shutting down. how can i figure it out?
the script is designed to be run in startup and when shutting down (rc.d)...
 
Old 12-02-2012, 03:08 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,169

Rep: Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493
if your monitor turns off
or the light on the power button turns off
or if the computer powers down ......
 
Old 12-02-2012, 03:43 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,561
Blog Entries: 13

Rep: Reputation: Disabled
Code:
#!/usr/bin/perl
use 5.010; 
use autodie;
</smart_aleck>
 
Old 12-02-2012, 03:44 PM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
It depends on your distribution, which scripts are executed when the system switches the runlevels.

Read the documentation for your distribution and find out which script runs when the system boots (for example in Slackware /etc/rc.d/rc.S) and when the system is shut down (in Slackware /etc/rc.d/rc.0)
Then put a line into these scripts which execute your perl-script.

Markus
 
Old 12-02-2012, 04:26 PM   #5
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
You could let the script take an argument. Then in the startup script you could give it the "start" argument and when called from the shutdown script you could give it the "stop" atgument. This is pretty much just like how any other init script works.
 
Old 12-03-2012, 12:02 AM   #6
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
thx 4 replying,
John: oh man! i know what is happening. the matter is that the perl script knows it too.
Habitual: hmm, u mean autodie causes the changed environment variables get applied to the whole shell?
i don't want the perl script be died abnormally. does it cause such a behavior?
markush & btmiller: it's Ubuntu 10.10. it's a written project we're applying changes on. a shell script located on /etc/rc0.d to /etc/rc6.d run a perl script with 'start' or 'stop' argument. the perl script uses a pm (perl module?) file named Config.pm. the decision must be made in the pm file which i don't know how is executed and i think the argument is not passed to it like the main perl script.
if you're familiar with ZoneMinder, zmpkg.pl is the main script and the Config.pm is located on /usr/share/perl/5.10/ZoneMinder.
about the shell script which starts with S20 or K20 in /etc/rcx.d, it's weird that just existence is selectable. otherwise, all these scripts indeed are the same, so that change in one affects of others!
 
Old 12-03-2012, 12:06 AM   #7
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
Habitual: i tried it; no change
 
Old 12-03-2012, 05:52 AM   #8
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,561
Blog Entries: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
It depends on your distribution, which scripts are executed when the system switches the runlevels.

Read the documentation for your distribution and find out which script runs when the system boots (for example in Slackware /etc/rc.d/rc.S) and when the system is shut down (in Slackware /etc/rc.d/rc.0)
Then put a line into these scripts which execute your perl-script.

Markus
Ignore me on that reply.
I was kidding.

Markus is spot-on.
What OS are you dealing with?
 
Old 12-03-2012, 08:19 AM   #9
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
i told u
it's ubuntu 10.10
 
Old 12-03-2012, 08:27 AM   #10
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
hamidi2, did you find out how Ubuntu organizes the startup scripts and the shutdown scripts? could you find out which script runs when you want your perlscript to be executed?

Markus
 
Old 12-03-2012, 10:31 PM   #11
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
yes, but the problem is that when the package is installed it creates links to '/etc/init.d/app.sh' in all rc*.d directories. i don't know how this happens and how to change this behavior. for example, if i know how to tell package installer to install separate shell scripts for startup and non-startup conditions, it may resolve my problem.
in another hand, i've heard that there's no difference in runlevel between when the system is starting up and when i manually run the script. in another words, i can't distinguish between them in the shell script.
the IDEAL is that the SYSTEM ITSELF decide to run the shell script at startup after MySQL gets ready to operate and acknowledge requests. if this may happen, i've not to check about it in the shell script at all and the problem will be resolved with a better behavior.
 
Old 12-04-2012, 06:35 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
It sounds like its installing correctly. All you need to do is adjust the SXX number of each entry in each dir so that it starts AFTER mysql, instead of before it (& vice versa for shutdown/KXX links)

Eg on my system, I have /etc/rc3.d/S64mysqld, so I'd edit newapp link to be S65newapp
 
Old 12-04-2012, 11:30 PM   #13
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
ur right, but in Ubuntu mysql starts differently. sysv is old and a new replacement is putting mysql.conf in /etc/init. i'm not sure which directory is paid attention first, /etc/init or /etc/init.d. but my script is old style. i've to use the same method u mentioned for it, but it doesn't differ what number i assign. i assigned 98 02 so that it be the later one. but sometimes it works and sometimes it doesn't.
note that even starting before mysql is not enough! because it takes time for mysql to get ready.
i used this method:
i wait 60 seconds in a loop in which i check mysql each second. the startup problem is solved.
the shutdown process executes the same script. when it reaches the wait loop mysql has ended. so shutdown process takes one minute to accomplish and times out. for this not to take place i check errorlevel:
Code:
if ($ENV{runlevel} == 6) { return; }
i hope mysql get ready to work in at most one minute in all systems.
 
  


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
Shutting down and non shutting down x windows golden_boy615 Linux - General 1 03-01-2010 06:28 AM
shutting down linux, please help. camladuk Linux - Software 8 04-29-2006 06:23 PM
shutting down PC - Linux Enterprise WS SsGeorge Linux - Enterprise 4 03-31-2006 03:16 PM
Shutting down windowsXP from linux? filpee Linux - General 2 02-20-2005 02:33 AM
shutting down linux from windows dave308 Linux - Networking 9 12-13-2001 08:09 PM

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

All times are GMT -5. The time now is 11:43 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