LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-20-2015, 09:43 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 761

Rep: Reputation: Disabled
Create shell script which could be started and stopped


I wish to create a shell script which calls a PHP function ever 5 seconds. I was able to do so, but I can't stop it. Now, I wish to modify it so that it could be started, stopped, restarted, and status could be requested (actually, I would be find with just start and stop) using something like the following:
Code:
[root@devserver ~]# /etc/init.d/my5SecondService start
[root@devserver ~]# /etc/init.d/my5SecondService stop
[root@devserver ~]# /etc/init.d/my5SecondService restart
[root@devserver ~]# /etc/init.d/my5SecondService status
I started using the httpd shell script as a guide, but am a little lost.

Can anyone either direct me to a good template to start from and/or review the following? The whole part about pid has me stumped.

Thanks

Code:
#!/bin/bash
# /etc/init.d/my5SecondService

### BEGIN INIT INFO
# Provides:          my5SecondService
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PHP Loop
# Description:       Calls PHP script every 5 seconds.
### END INIT INFO

# Get function from functions library. WHY?
. /etc/init.d/functions

start() {
        initlog -c "echo -n Starting my5SecondService server: "
        
        while true
        do
         /usr/bin/php /var/www/my5SecondService.php &
          sleep 5
          done
          exit 0
          
        ### Create the lock file ###   WHY???
        touch /var/lock/subsys/my5SecondService
        success $"my5SecondService server startup"
        echo
}

stop() {
        initlog -c "echo -n Stopping my5SecondService server: "
        killproc my5SecondService
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/my5SecondService
        echo
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status my5SecondService
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0

Last edited by NotionCommotion; 12-20-2015 at 07:54 PM.
 
Old 12-20-2015, 12:09 PM   #2
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 761

Original Poster
Rep: Reputation: Disabled
Hello again, If I execute /etc/init.d/simulated_bacnet_device start, the loop starts but status shows it as being stopped. Thanks for any suggestions.
 
Old 12-20-2015, 07:49 PM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 761

Original Poster
Rep: Reputation: Disabled
Please?
 
Old 12-20-2015, 11:39 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
A web search for "linux simple init script" turns up a lot of hits. Here is a good one.

Although your loop needs to be in another file that you execute from the init script. i.e. the myapp in the link above would be something like this:
Code:
#!/bin/bash
while true
do
    /usr/bin/php /var/www/my5SecondService.php
    sleep 5
done
exit 0
You didn't say what your php script is doing but if you use a pid file, like the template in the link, you can do the loop/sleep in the php script and execute it directly from the init script.

HTH
 
Old 12-21-2015, 08:50 AM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 761

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
Although your loop needs to be in another file that you execute from the init script.
Thank you norobro,

Why does the loop need to be in another file?

Maybe putting the loop in the PHP script makes more sense, however, my end goal is to get rid of the PHP file and implement it all in the init script.

Also, I actually had done several searches, and found several examples which I used to follow, however, I don't know if they were good ones.

Thanks again
 
Old 12-21-2015, 11:08 AM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
I have never thought of an init script as controlling another instance of itself rather than a completely separate process. But I guess it is possible.

From your start function:
Code:
start() {
        initlog -c "echo -n Starting my5SecondService server: "
        
        while true
        do
         /usr/bin/php /var/www/my5SecondService.php &
          sleep 5
          done
          exit 0
          
        ### Create the lock file ###   WHY???
        touch /var/lock/subsys/my5SecondService
        success $"my5SecondService server startup"
        echo
}
The loop never exits so you never reach the code to create a lock file. Moving the touch statement to before the loop would solve that, but you need to capture the pid of the running instance and write it to a file. Then when you execute the the script with 'stop' or 'status', creating another instance, it will read the pid file and kill, or check for, the script running the loop. Also, you will need to check for a pid file in the start function to ensure that you don't start multiple instances.

Quote:
# Get function from functions library. WHY?
. /etc/init.d/functions
On my Debian machine those functions are in /lib/lsb/init-functions. That's where kill_proc, status_of_proc(status), etc. reside. I'm not familiar with them but they require a pidfile to be passed in.

With a separate script you could do something like this:
Code:
start() {
        if [ -f /var/run/my5SecondService ] ; then
    	    echo "my5SecondService already running"
    	    exit 0;
    	fi
        echo "Starting my5SecondService"
        /path/to/my5SecondService.sh &    # separate executable script with loop - see post #4
        touch /var/run/my5SecondService
        exit 0
}

stop() {
        kill  $(pidof -x  my5SecondService.sh)
        rm -f /var/run/my5SecondService
        echo "Stopping my5SecondService"
        exit 0
}

status() {
	if [ -f /var/run/my5SecondService ] ; then
	    echo "my5SecondService is running"
	else
	    echo "my5SecondService is not running"
	fi
	return 0
}

Last edited by norobro; 12-21-2015 at 08:48 PM. Reason: spotted a typo
 
Old 12-21-2015, 09:48 PM   #7
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 761

Original Poster
Rep: Reputation: Disabled
Thank you norobro, Look forward to trying it out. Michael
 
  


Reply

Tags
initd, shell script


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
How to create a script that kills all processes started by a specific user? Squerl101 Linux - General 12 02-19-2013 07:09 AM
Autograder Shell Script-- hints on getting started? calvarado777 Linux - Newbie 5 10-19-2012 08:45 PM
Shell Script which track a service which is stopped bydefault Nilesh General 3 11-24-2011 08:04 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
Help: Create a shell script to move only files wich has stopped growing proxmity Linux - Newbie 8 07-28-2009 12:40 PM

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

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