LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-08-2015, 10:07 AM   #1
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Rep: Reputation: Disabled
startup script does not start


Hello everyone !
I want to run script at start up. So i wrote script following to this article
http://www.tldp.org/HOWTO/HighQualit...OWTO/boot.html
script looks like this :

Note: for testing purpose i grep -c 'still'
Code:
    #!/bin/bash
    #
    # /etc/rc.d/init.d/mysystem
    # Subsystem file for "MySystem" server
    #
    # chkconfig: 2345 95 05
    # description: MySystem server daemon
    #
    # processname: MySystem
    # config: /etc/MySystem/mySystem.conf
    # config: /etc/sysconfig/mySystem
    # pidfile: /var/run/MySystem.pid

    # source function librery
    . /etc/rc.d/init.d/functions

    # pull in sysconfig settings
    [ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem

    RETVAL=0
    prog="MySystem"

    start() {
          echo -n $"Starting $prog: "
          if [ "$(last | head -1 | grep -c 'still')" == 1 ]
             then
                  printf "%b\n" "system crashed"
                  $(mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.backup)
                  $(rm -f /var/lib/mysql/mysql.sock)
                  if [ $? == 0 ]
                          then
                          printf "%b\n" "mysql.sock deleted successfuly"
                           $(shutdown -r 3)
                  fi
          fi
          #RETVAL=$?
          #[ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
          #echo
    }

    stop() {
          echo -n $"Stopping $prog: "
          #
          #
          #
          killproc $porg -TERM
          RETVAL=$?
          [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
          echo
    }

    reload() {
          echo -n $"Reloading $prog: "
          killproc $prog -HUP
          RETVAL=$?
          echo
    }

    case "$1" in
       start)
             start
             ;;
       stop)
             stop
             ;;
       restart)
             stop
             start
             ;;
       reload)
             reload
             ;;
       condrestart)
             if [ -f /var/lock/subsys/$prog ] ; then
                   stop
                   # avoid race
                   sleep 3
                   start
             fi
             ;;
       status)
             status $prog
             RETVAL=$?
             ;;
       *)
             echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
             RETVAL=1
    esac
    exit $RETVALT
then i created symbolic link in /etc/rc5.d/
Code:
    <<root|red:/etc>># ls -l rc5.d/S95mysystem
    lrwxrwxrwx. 1 root root 18 Jun  8 13:03 rc5.d/S95mysystem -> ../init.d/mysystem
but still not ...
then i added command to /etc/rc.local
Code:
    <<root|red:/etc>># cat rc.local
    #!/bin/bash
    # This script will be executed *after* all the other init scripts.
    # You can put your own initialization stuff in here if you don't
    # want to do the full Sys V style init stuff.

    service mysystem start
    touch /var/lock/subsys/local
but still not ...
if i start service via command line it's works.
what i did wrong ?
 
Old 06-08-2015, 12:05 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Nothing apparent, the link in rc5.d should've taken care of it.

Is the script owned by root and also executable? How about an ls -l of the script itself?

To run the service out of rc.local, you do not need to start with the "service" command, instead you would just:
Code:
# mysystem start
Either case, the rc5.d should have worked. Are there other scripts run out of rc5 that you know are executing?
 
Old 06-08-2015, 02:06 PM   #3
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
startup script does not start

Code:
<<root|red:~>># ls -l /etc/init.d/mysystem
-rwxr-xr-x. 1 root root 1573 Jun  8 14:54 /etc/init.d/mysystem
for exmple mysqld from /etc/rc5.d/S64mysqld
Code:
<<root|red:~>># service mysqld status
mysqld (pid  2301) is running...
 
Old 06-08-2015, 02:17 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I was thinking earlier to get fundamental and add like an echo to a log file line right at the top of your script.
Code:
   #!/bin/bash
    #
    # /etc/rc.d/init.d/mysystem
    # Subsystem file for "MySystem" server
    #
    # chkconfig: 2345 95 05
    # description: MySystem server daemon
    #
    # processname: MySystem
    # config: /etc/MySystem/mySystem.conf
    # config: /etc/sysconfig/mySystem
    # pidfile: /var/run/MySystem.pid

    echo "The script is running!" > /home/<your-username>/mysystem.log
As I say, no reason that should not be running.
 
Old 06-08-2015, 02:33 PM   #5
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
i've raplaced service mysystem start to mysystem start in /etc/rc.local

and here is /var/log/boot.log tail :
Code:
Starting MySystem: Starting libvirtd daemon: 2015-06-08 19:02:33.890+0000: 2506: info : libvirt version: 0.10.2, package: 29.el6 (CentOS BuildSystem <http://bugs.centos.org>, 2013-11-23-13:07:07, c6b8.bsys.dev.centos.org)
2015-06-08 19:02:33.890+0000: 2506: warning : virGetHostname:2294 : getaddrinfo failed for 'red': Name or service not known
                                                           [  OK  ]
Starting up CIM server:                                    [  OK  ]
Starting certmonger:                                       [  OK  ]
/etc/rc5.d/S99local: line 6: mysystem: command not found
 
Old 06-08-2015, 02:35 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by crocodile9 View Post
i've raplaced service mysystem start to mysystem start in /etc/rc.local

and here is /var/log/boot.log tail :
Code:
Starting MySystem: Starting libvirtd daemon: 2015-06-08 19:02:33.890+0000: 2506: info : libvirt version: 0.10.2, package: 29.el6 (CentOS BuildSystem <http://bugs.centos.org>, 2013-11-23-13:07:07, c6b8.bsys.dev.centos.org)
2015-06-08 19:02:33.890+0000: 2506: warning : virGetHostname:2294 : getaddrinfo failed for 'red': Name or service not known
                                                           [  OK  ]
Starting up CIM server:                                    [  OK  ]
Starting certmonger:                                       [  OK  ]
/etc/rc5.d/S99local: line 6: mysystem: command not found
Get the path for mysystem and put it in explicitly in the script.
Code:
which mysystem
will show you the path.
 
Old 06-08-2015, 02:49 PM   #7
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
which didn't found mysystem, i've just put it into /etc/init.d/

Code:
<<root|red:~>># which mysystem
/usr/bin/which: no mysystem in (/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
 
Old 06-08-2015, 03:06 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,676

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
What are you trying to accomplish?

Is there an actual mysystem program other then the startup script you created. Are you just wanting to check the status of mysql and restart the computer if not running? Does mysql frequently crash on boot up?

You could just move that part of the script to rc.local.
 
Old 06-08-2015, 03:20 PM   #9
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
No, there is no actual mysystem program, this is just script. i want to check if there wasn't normal system down, this is marked as 'crash' in first line last command output , then i try to remove old mysql.sock, which is automatically created at next system boot by mysql. this is case of webserver 24/7 uptime. sometimes occours power loss.
this might be solved in different ways. but i want to know why is not working ?
 
Old 06-08-2015, 03:41 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,676

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
There isn't a path environment yet so your if statement is failing (if [ "$(last | head -1 | grep -c 'still')" == 1 ]). You can add the path like below to your script.

PATH=/usr/bin
 
Old 06-09-2015, 02:55 AM   #11
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
There isn't a path environment yet so your if statement is failing (if [ "$(last | head -1 | grep -c 'still')" == 1 ]). You can add the path like below to your script.

PATH=/usr/bin
so i changed my script to this:
Code:
[laser@red~]$ cat mysystem2
#!/bin/bash

PATH=/usr/bin:/sbin:/bin:/home/laser

echo "-------------------------------" >> /home/laser/mysystem2.log 
echo "$PATH" >> /home/laser/mysystem2.log
echo "mysystem2 is running" >> /home/laser/mysystem2.log 

if [ "$(last | head -1 | grep -c 'still')" == 1 ]
	then
		echo "last, head, grep succeed" >> /home/laser/mysystem2.log
		$(service mysqld stop)
       		echo "service mysqld status" >> /home/laser/mysystem2.log
		$(mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bakup)
		if [ $? == 0 ]
			then
				echo "mv succeed" >> /home/laser/mysystem2.log
		fi

       		$(rm -f /var/lib/mysql/mysql.sock)
		if [ $? == 0 ]
			then
				echo "rm succeed" >> /home/laser/mysystem2.log
		fi

		$(shutdown -r 2)
fi
permissions are:
Code:
<<root|red:/home/laser>># ll mysystem2
-rwxrwxr-x. 1 root root 819 Jun  9 10:38 mysystem2
rc.local :
Code:
<<root|red:/home/laser>># cat /etc/rc.local 
#!/bin/bash
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

/home/laser/mysystem2
touch /var/lock/subsys/local
mysystem2.log :
Code:
<<root|red:/home/laser>># cat mysystem2.log
------------------------------- 
/usr/bin:/sbin:/bin:/home/laser
mysystem2 is running
and extra :
/var/log/boot.log last 3 lines
Code:
Starting up CIM server:                                    [  OK  ]
Starting certmonger:                                       [  OK  ]
rc5.d/ low priority services
Code:
lrwxrwxrwx. 1 root root 20 Jun  8 13:03 S99certmonger -> ../init.d/certmonger
lrwxrwxrwx. 1 root root 24 Jun  8 13:03 S99libvirt-guests -> ../init.d/libvirt-guests
lrwxrwxrwx. 1 root root 11 Nov 26  2014 S99local -> ../rc.local
(why S99local script's startup message is not logging in /var/log/boot.log ?)

even i've added it to System -> Preferences -> Startup Applications

still doesn't runs ...

Last edited by crocodile9; 06-09-2015 at 04:29 AM.
 
Old 06-09-2015, 06:16 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,881
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Get more fundamental and debug with some coherence.

You say the script runs when you execute it manually and also that you're having difficulties in running it out of rc.local and/or trying to run it as part of the rc5.d init sequence. So debug piece by piece.
  1. Write a very simple script that just echo's a log to a file
  2. Call that script from rc.local and verify that it does or doesn't work
  3. Similarly verify that you can create a symbolic link in rc5.d and cause that test script to run, or if that doesn't work
  4. Presuming that you can successfully invoke a script from rc.local or rc5.d, then what is next is to determine why your particular script does not run correctly when invoked from rc.local
    1. Incorporate added debug to show that you've entered your script
    2. Add debug to determine the result codes for when you run specific commands and also output that information to a log file
    3. Determine the environment circumstances for when you run the script manually to assist you in why the script works manually but not via rc.local
    4. When running manually, add a "set -xv" line to the top of your script, but below the #!/bin/bash line. This will enable debug and show you verbose output as to what happens when you run locally, and this may give you an idea where the path for things are, or what defined variables are of consequence when running the script
  5. See the My Bash Blog link in my signature for added tips on BASH scripting

Last edited by rtmistler; 06-09-2015 at 06:18 AM.
 
Old 06-09-2015, 08:58 AM   #13
crocodile9
LQ Newbie
 
Registered: Jun 2015
Distribution: CentOS, Debian
Posts: 7

Original Poster
Rep: Reputation: Disabled
mistake was at the very top in script -> Note: for testing purpose i grep -c 'still'
that has been flowing to down.
i amended the first line :
Code:
#!/bin/bash -xv
and this showed up that evaluation of expression
Code:
[ "$(last | head -1 | grep -c 'still')" == 1 ]
was false, because until logged in system, there is no line in last command output containing - still,
very top line is
Code:
reboot   system boot  2.6.32-431.el6.x Tue Jun  9 15:17 - 15:38  (00:21)
Code:
last | head -1 | grep -c 'still')"
last | head -1 | grep -c 'still')
last | head -1 | grep -c 'still'
++ grep -c still
++ last
++ head -1
+ '[' 0 == 1 ']'
So it's solved !
from /etc/init.d/mysystem works also.

i solved this by myself, but thanks anyway.

Last edited by crocodile9; 06-09-2015 at 09:41 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SOLVED: startup script will not start process tonj Linux - Newbie 1 05-16-2014 04:54 PM
Start python application (MoinMoin) at startup / by running a script xbeck Linux - Newbie 6 07-10-2011 01:28 AM
start a daemon script at startup haxxor23 Linux - Newbie 2 05-12-2007 07:59 AM
start own script during the startup automatically cccc SUSE / openSUSE 11 03-13-2006 03:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:07 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
Open Source Consulting | Domain Registration