Solved

Ubuntu Sever Application Startup

Posted on 2011-02-21
14
492 Views
Last Modified: 2012-05-11
I need to run an application on my server at the startup.

1- I copied my script in /etc/init.d, like so:
sudo cp /usr/app/app01.x86 /etc/init.d/app01

2- Then made it executable:
sudo chmod a+x app01

3- Then informed the system that I wish the app01 to run automatically on server startup, using:
sudo update-rc.d app01 defaults

I got this message:
root@app01svr01:~# sudo update-rc.d app01 defaults
update-rc.d: warning: /etc/init.d/app01 missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/app01 ...
   /etc/rc0.d/K20app01 -> ../init.d/app01
   /etc/rc1.d/K20app01 -> ../init.d/app01
   /etc/rc6.d/K20app01 -> ../init.d/app01
   /etc/rc2.d/S20app01 -> ../init.d/app01
   /etc/rc3.d/S20app01 -> ../init.d/app01
   /etc/rc4.d/S20app01 -> ../init.d/app01
   /etc/rc5.d/S20app01 -> ../init.d/app01
   
5- I rebooted the server to check that the app01 was automatically starting, and actually it was, but was loaded as S20app01.
The problem is that I created two scripts to stop/restart the application:

Stop Scrip:
#!/bin/sh
killall -9 app01

Restart:
#!/bin/sh
killall -9 app01
sleep 1
ulimit -c unlimited -n 65536
/usr/app/app01 &


As you can see on the scripts I stop/restart the proccess called app01, but the app01 is starting up as a process called S20app01 as mentioned above.


What can I do to assure that app01 will always start up as the proccess called "app01" instead of something else.
Or there is other way to accomplish this?



Thanks
0
Comment
Question by:vianneyjs03
  • 6
  • 5
  • 3
14 Comments
 
LVL 12

Expert Comment

by:upanwar
ID: 34942811
No need to worry.
When we add chkconfig to any script to start or stop at the time of booting. It creates soft links in run levels directory, the file which name start with S and K, S stands for starting of service in run level and K stand for stopping of service.

Nos like 20 denotes the sequence of starting and stopping service.
0
 
LVL 12

Expert Comment

by:upanwar
ID: 34942871
0
 

Author Comment

by:vianneyjs03
ID: 34942888
Thanks upanwar.

So, should I change my scripts to this?

Stop Script:
#!/bin/sh
killall -9 S20app01

Restart Script:
#!/bin/sh
killall -9 S20app01
sleep 1
ulimit -c unlimited -n 65536
/usr/app/app01 &

Is there any chances that this "S20app01" process name change in the future?


Thanks a lot.
0
 
LVL 12

Expert Comment

by:upanwar
ID: 34942907
no need to change anyting.
0
 
LVL 76

Expert Comment

by:arnold
ID: 34942929
chkconfig --list app01 it should tell you on which run levels it will run and on which run levels it will not.

As upanwar pointed out the symlinks is how the main /etc/init.d/app01 is linked to each run level rc2.d rc3.d rc4.d rc5.d rc6.d

presumably in your app01 script you have a line:
#chkconfig [run_levels_to_run] [start number] [stop number]
i.e. if you want it to run at run levels 345, it should start in the middle (0-100) 50 a and stop in the middle 50 your line will be
#chkconfig 345 50 50
The consideration for the start/stop order deal with whether the resources it needs were already started. i.e. if your application relies on network, mysql, etc. its start number can not be lower their those services nor can it be higher than those services on the stoppage.
i.e. network is 10 90
mysql is 64 36
If app01 need mysql to work, it can not start before 64 not can it be stopped after 36.
in this case your app01 chkconfig line should be:
#chkconfig 345 70 30

You should not use kill -9 as it does not provide your application any chance to clean up after itself i.e. finish transactions it was doing and depending on what the application is doing, could lead to data loss.
kill -15 (TERM) is a better approach that lets the application know that it needs to finish up and exit.
0
 

Author Comment

by:vianneyjs03
ID: 34942947
That's the reason why I'm asking this question, sorry  didn't mention it.

When I run the stop/restart script i got this message:
"process not found" because the application is starting up as "S20app01" instead of "app01", I checked this with the Sytem Monitor.

0
 
LVL 76

Expert Comment

by:arnold
ID: 34943079
The S20app01 is not the name of the application that runs. S20app01 is merely a script that starts the application and based on the entry you posted the application is run as /usr/app/app01

Note that using a to broad a kill directive could get the script terminated
i.e. a script S20app01 with a directive within saying

PID=`ps -ef | grep app01`
kill -TERM $PID
Could have a list of PIDs that includes this script and it could be the first killed.

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:vianneyjs03
ID: 34943166
Hi Arnold.

I'm a little bit confused (I'm a Ubuntu/scripting nuewbie), what you said is that i should use this script:
PID=`ps -ef | grep app01`
kill -TERM $PID

To kill the "S20app01" process that is running
0
 
LVL 76

Expert Comment

by:arnold
ID: 34943239
No you should not have an S20app01 running, you should have a /usr/app/app01 running.
S20app01 will only be running if /usr/app/app01 is not really a daemon i.e. it does not detach and through itself into the background to do that, you should add an ampersand (&) on the /usr/app/app01 line in the script
/usr/app/app01 &

This way S20app01 will be done when app01 is run in the background.
0
 

Author Comment

by:vianneyjs03
ID: 34943350
I don't think that app01 is a deamon as you say.
So the stop script should be something like:

#!/bin/sh
killall -9 /usr/app/app01 &


Please advise.
0
 
LVL 76

Expert Comment

by:arnold
ID: 34943462
No, the start of the app01 should have the &
There is no point of sending the termination process into the background.

start )

do stuff
/usr/app/app01 & #starting the application and sending it to the background

stop )
   kill -TERM /usr/app/app01

restart)
      $0 stop
      $0 start
0
 

Author Comment

by:vianneyjs03
ID: 34943501
Ok, thanks Arnold for your outstanding support, I'll check this and get back to you.


Regards.
0
 

Author Comment

by:vianneyjs03
ID: 34953026

Hi Arnold.

Everytime I've tried to run the script you suggested, I got this error:


# sudo /etc/init.d/app01
/etc/init.d/app01: 2: Syntax error: ")" unexpected
0
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
ID: 34956398
usually you have to use a parameter such as start/stop etc.

I provided a general format of what a init.d script might look like. It is in no way a complete script.

look at any of the existing ones that you have there.
The script has to take care and set all the environment variables needed for the application.
It then has a check to see whether any argument was passed
/etc/init.d/nfs as an example and this is still incomplete but might be closer to explain the issue or how/what you should do.
etc.
#or have the case condition check
#!/bin/sh

#chkconfig 345 80 20
#start as close to the end as possible (80), stop as close to the begining of the shutdown/restart process as possible (20).

#set parameters

case "$1" in
start)
   /usr/app/app01 &
  #what to do when the command is to start
;;
stop)
#what to do when the command is to stop
PID=`ps -ef | grep '/usr/app/app01' | grep -v 'grep'`
kill -TERM "$PID"
;;

*)
#matches all that fell through and this is where you would output the directives
echo "Usage: $0 (start|stop|restart|reload)"
RETVAL=1 #error when return value is anything but 0.
;;
esac
exit $RETVAL
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now