LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 07-19-2011, 03:33 PM   #1
FeyFre
Member
 
Registered: Jun 2010
Location: Ukraine, Vinnitsa
Distribution: Slackware
Posts: 308

Rep: Reputation: 22
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).
 
Old 07-19-2011, 04:23 PM   #2
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Rep: Reputation: 534Reputation: 534Reputation: 534Reputation: 534Reputation: 534Reputation: 534
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
 
Old 07-19-2011, 06:03 PM   #3
FeyFre
Member
 
Registered: Jun 2010
Location: Ukraine, Vinnitsa
Distribution: Slackware
Posts: 308

Original Poster
Rep: Reputation: 22
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).
 
Old 07-19-2011, 06:41 PM   #4
piratesmack
Member
 
Registered: Feb 2009
Distribution: Slackware, Arch
Posts: 518

Rep: Reputation: 133Reputation: 133
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.

Last edited by piratesmack; 07-20-2011 at 01:08 AM.
 
Old 07-19-2011, 06:49 PM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 11,197
Blog Entries: 3

Rep: Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425Reputation: 1425
Hi,

Quote:
Originally Posted by FeyFre View Post
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!
 
Old 07-20-2011, 12:44 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 07-20-2011, 04:29 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,755

Rep: Reputation: 466Reputation: 466Reputation: 466Reputation: 466Reputation: 466
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.
 
Old 07-20-2011, 08:57 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by gnashley View Post
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.
 
1 members found this post helpful.
Old 07-20-2011, 12:32 PM   #9
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,431

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
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.
 
Old 07-20-2011, 01:53 PM   #10
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,755

Rep: Reputation: 466Reputation: 466Reputation: 466Reputation: 466Reputation: 466
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...
 
1 members found this post helpful.
Old 07-20-2011, 05:01 PM   #11
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 227Reputation: 227Reputation: 227
Just to refine what allend suggested

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


Reply

Tags
auto, boot, patch, system


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
Add a script to startup/init? licht Suse/Novell 2 07-08-2007 08:53 AM
Add an init script? rogueeve Linux - Newbie 1 05-17-2007 02:53 PM
how do i add my script in init nukeu666 Linux - Newbie 1 08-16-2004 07:56 AM
How to add something in init script Ypkis Linux - Newbie 5 10-12-2003 06:35 AM
Script thats add users automatically embalmedlenin *BSD 1 09-14-2003 04:34 PM


All times are GMT -5. The time now is 11:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration