LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Smooth way to add init script automatically. (http://www.linuxquestions.org/questions/slackware-14/smooth-way-to-add-init-script-automatically-892574/)

FeyFre 07-19-2011 03:33 PM

Smooth way to add init script automatically.
 
Hello

I have a task: install some software package. I have wrote SlackBuild which builds package. doinst.sh does some configuration actions. The second task of doinst.sh - to make software start automatically during system bootup(have ready /etc/rc.d/rc.mysoft script). A good place start it is /etc/rc.d/rc.local (and shutdown in /etc/rc.d/rc.local_shutdown).

Question: how to do it? How in bash/sh script correctly patch rc.local and rc.local_shutdown ? And how correctly unpatch it? I can do it manually, but I interested in automatic mechanism. The task is complicated by fact I'm going to install more than one of such (independent)packages, and start/stop sequences must be in strict order(i.e start A, then B, then C, then D, and this order should be strict independently of package installtion order).

Woodsman 07-19-2011 04:23 PM

Quote:

The second task of doinst.sh - to make software start automatically during system bootup(have ready /etc/rc.d/rc.mysoft script).
Quote:

start/stop sequences must be in strict order(i.e start A, then B, then C, then D, and this order should be strict independently of package installtion order).
A common way is to edit /etc/rc.d/rc.local. Something like this:

if [ -x /etc/rc.d/rc.mysoft-A ]; then
/etc/rc.d/rc.mysoft-A start
fi
if [ -x /etc/rc.d/rc.mysoft-B ]; then
/etc/rc.d/rc.mysoft-B start
fi
if [ -x /etc/rc.d/rc.mysoft-C ]; then
/etc/rc.d/rc.mysoft-C start
fi
if [ -x /etc/rc.d/rc.mysoft-D ]; then
/etc/rc.d/rc.mysoft-D start
fi

Then ensure all the new scripts are executable (chmod +x).

If you want to methodically disable those processes when powering down and order is important, then edit rc.local_shutdown and reverse the order used in rc.local. Something like this:

if [ -x /etc/rc.d/rc.mysoft-D ]; then
/etc/rc.d/rc.mysoft-D stop
fi
if [ -x /etc/rc.d/rc.mysoft-C ]; then
/etc/rc.d/rc.mysoft-C stop
fi
if [ -x /etc/rc.d/rc.mysoft-B ]; then
/etc/rc.d/rc.mysoft-B stop
fi
if [ -x /etc/rc.d/rc.mysoft-A ]; then
/etc/rc.d/rc.mysoft-A stop
fi

FeyFre 07-19-2011 06:03 PM

Woodsman, I know how to edit rc.local and rc.local_shutdown using vi or other interactive editor! I know how to type "chmod +x /etc/rc.d/rc.mysoft" with my hands in shell also.
I need it to be done by script automatically. I.e. I type ./install-init-scripts.sh (or it will be part of doinst.sh) and this script must do next:
1. Find /etc/rc.d/rc.local and /etc/rc.d/rc.local_shutdown (is not present - create them)
2. Check if there is already present start/stop loader (i.e if [ -x ... ]; then with target starter and stopper) and if check is failed, it should insert it into right place
3. It must ensure the start/stop load is placed on right place i.e B must be run before C and after A.
This script should do it automatically without interaction with user. That is my problem.

Yes, I know I can use SysV style runlevel management, but it is not an option in my case(it is a crutch).

piratesmack 07-19-2011 06:41 PM

Could maybe do something like this in the doinst.sh

Code:

if ! grep -q '^#Added automatically by mysoft$' etc/rc.d/rc.local ; then
  cat <<EOF >> etc/rc.d/rc.local
#Added automatically by mysoft
if [ -x /etc/rc.d/rc.mysoft ]; then
  /etc/rc.d/rc.mysoft start
fi
EOF
fi

edit: That's not quite what you want. I apologize for only skimming through this thread before replying.

onebuck 07-19-2011 06:49 PM

Hi,

Quote:

Originally Posted by FeyFre (Post 4419382)
Hello
I have a task: install some software package. I have wrote SlackBuild which builds package. doinst.sh does some configuration actions. The second task of doinst.sh - to make software start automatically during system bootup(have ready /etc/rc.d/rc.mysoft script). A good place start it is /etc/rc.d/rc.local (and shutdown in /etc/rc.d/rc.local_shutdown).

Question: how to do it? How in bash/sh script correctly patch rc.local and rc.local_shutdown ? And how correctly unpatch it? I can do it manually, but I interested in automatic mechanism. The task is complicated by fact I'm going to install more than one of such (independent)packages, and start/stop sequences must be in strict order(i.e start A, then B, then C, then D, and this order should be strict independently of package installtion order).

Since you state that task(s) can be performed manually from the console then;
Quote:

excerpt 'man script';
script -- make typescript of terminal session

SYNOPSIS
script [-a] [-c COMMAND] [-f] [-q] [-t] [file]

DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for
students who need a hardcopy record of an interactive session as proof of an assign-
ment, as the typescript file can be printed out later with lpr(1).

If the argument file is given, script saves all dialogue in file. If no file name is
given, the typescript is saved in the file typescript.

Options:

-a Append the output to file or typescript, retaining the prior contents.

'script -a' will output your entries to typescript or you could designate a filename.

That way you have a start for creating a script. Sure you will need to perform conditional and script format to get things going. It is a start since you can manually do the task(s).

Just a few links to aid you to gaining some understanding;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide
10 Linux Home Networking
11 Virtualization- Top 10

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!

catkin 07-20-2011 12:44 AM

So any of the packages installations can insert the code Woodsman suggested as piratesmack suggested. For uninstalling, either the code could be removed by the last package out (each installation would need a mechanism to detect any of the other packages) or it could be left there; the if [ -x tests would render it ineffective. If you go for the first option it would be helpful to add a comment line telling the user not to modify any of the inserted/appended lines if they want the uninstallation procedure to be effective.

gnashley 07-20-2011 04:29 AM

I use code like this:
Code:

if ! grep -q '/etc/rc.d/rc.mysoft start' etc/rc.d/rc.local |grep -v # ; then
  cat <<EOF >> etc/rc.d/rc.local
#Added automatically by mysoft
if [ -x /etc/rc.d/rc.mysoft ]; then
  /etc/rc.d/rc.mysoft start
fi
EOF
fi

so that the doinst checks specifically for the rc.mysoft entry -this allows you to use similar code for other scripts. The 'grep -v #' filters out any commented entries. src2pkg automatically creates such doinst code for any non-standard package which contains an rc.* file. The same sort of thing could be done for local_shutdown, but as already pointed out, the code checks if the rc.* is executable which implies that it is present.

Unfortunately this does *not* work for multiple services which must be started in a certain order unless they are installed in the correct order. If the services are all interdependent, then you could devise some code within the main rc.* file which would take care of starting all the services in the correct order. Slackware package management doesn't help you at all here since it doesn't understand dependencies.

catkin 07-20-2011 08:57 AM

Quote:

Originally Posted by gnashley (Post 4419936)
Unfortunately this does *not* work for multiple services which must be started in a certain order unless they are installed in the correct order. If the services are all interdependent, then you could devise some code within the main rc.* file which would take care of starting all the services in the correct order. Slackware package management doesn't help you at all here since it doesn't understand dependencies.

How about any of the interdependent-sequence packages installs the startup commands in the correct sequence for the whole set? The if [ -x tests mean the code is ineffectual for any not installed.

allend 07-20-2011 12:32 PM

Why not wrap the startup (and shutdown) commands into a for .. done loop?
Code:

for i in A B C D; do if [ -x /etc/rc.d/rc.mysoft-$i ]; then /etc/rc.d/rc.mysoft-$i; fi; done
The startup order is handled and it can be done as one line which is easy to check for presence using grep by the individual install scripts.

gnashley 07-20-2011 01:53 PM

Good ideas fellows. Anything that will help get things running correctly and that can be installed programatically -here we see an example of why slack doesn't try managing dependencies...

mRgOBLIN 07-20-2011 05:01 PM

Just to refine what allend suggested

Code:

ls -1 rc.mysoft-* |while read rcfile; do
  if [ -x "$rcfile" ]; then
        $rcfile start
  fi
done



All times are GMT -5. The time now is 04:48 AM.