LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Script similar to service command in Debian (http://www.linuxquestions.org/questions/slackware-14/script-similar-to-service-command-in-debian-4175439671/)

mlpa 12-02-2012 04:59 AM

Script similar to service command in Debian
 
1 Attachment(s)
Hi, I used some Debian servers in my University and one of the commands that I like is the service command.

I just write service the name of the script and the arguments. It is useful since I have a lot of deployment scripts that use SSH.

Now I want to change my server to Slackware :)
But I wanted to preserve the deployment script.

So here is my script that tries to emulate the service command in Debian.

Any suggestion is welcome, and feel free to use it.

Quote:

#!/bin/bash

PRG=`basename $0`
SERVICE_PATH="/etc/rc.d/"
PREFIX="rc."
N_ARGS=1

### Error codes based on linux error codes
ERROR_BAD_ARGS=22
ERROR_SERVICE_NOT_FOUND=2
ERROR_SERVICE_NOT_EXECUTABLE=1

if [[ $# < $N_ARGS ]]; then
echo "Usage: $PRG \"service name\" \"service args\""
exit $ERROR_BAD_ARGS
fi

ARRAY=("$@")
ARGS=""

for (( i=1; i<$#; ++i))
do
ARGS="$ARGS ${ARRAY[$i]}"
done

if [ -e "$SERVICE_PATH$1" ]; then
SERVICE="$SERVICE_PATH$1"
elif [ -e "$SERVICE_PATH$PREFIX$1" ]; then
SERVICE="$SERVICE_PATH$PREFIX$1"
else
echo "Service not found"
exit $ERROR_SERVICE_NOT_FOUND
fi

if [ -x $SERVICE ]; then
SERVICE="$SERVICE $ARGS"
else
echo "Service not executable"
exit $ERROR_SERVICE_NOT_EXECUTABLE
fi

exec $SERVICE
Another command that I am planning to emulate is insserv.
Giving a service script it copies to rc.d and add a line to rc.local.
Something like this.

rouvas 12-03-2012 04:39 PM

Your deployment (of what?) depends on starting services on machines?
What are you deploying?

It seems pointless to me.

wildwizard 12-04-2012 03:42 AM

Correct me if I'm wrong but isn't the service command a RedHat derivative?

Any way you'll be happy to know that Slackware has the ability to start and stop scripts that use the sysvinit style used by other distros, you'll just need to write a script that sets them up in the right directories and they will run automatically.

See /etc/rc.d and its sub-directories.

mlpa 12-05-2012 09:55 AM

Quote:

Originally Posted by rouvas (Post 4842048)
Your deployment (of what?) depends on starting services on machines?
What are you deploying?

It seems pointless to me.

By deploying I meant, compile code, pack a jar or a binary rsync with the server machine added the new service script and restart the service.
I have scripts that do all of this.
Until now the server machines were always Debian or Ubuntu distros, so my scripts have a line similar to service "service" start.
Since I want portability I am emulating this command.

Apart for portability there is another issue: Slackware is not that widely used (as sad as it is)
So I can not rewrite my deploy scripts because there are other persons involved that do not have contact with Slackware.

In summary portability and a common interface for my lab.

Quote:

Correct me if I'm wrong but isn't the service command a RedHat derivative?

Any way you'll be happy to know that Slackware has the ability to start and stop scripts that use the sysvinit style used by other distros, you'll just need to write a script that sets them up in the right directories and they will run automatically.

See /etc/rc.d and its sub-directories.
Maybe service command is from RedHat.
I can only say it is available in Debian.
The issue is not running the script, the issue is to have a deploy script that deploys in Slackware and (at least) Debian distros.

I am not saying it is useful for everyone.
If you like the idea use it, if you have fixes to the script please tell me.
If you do not need this emulation do not use it.

We have a choice now :)

FeyFre 12-05-2012 05:12 PM

And again we have here Yet-Another-Implememntation-Of-service-Command-For-Slackware.


PS: Yes, service is RedHat's(and derivatives) command.

rouvas 12-06-2012 03:13 AM

I still don't get it.
Why do you want to be distro-specific?

Why don't you follow the SysV way of placing your startup/shutdown scripts in /etc/rc.d/rc3.d (example for runlevel 3) and you have to follow somebody else's bright idea? Or you can follow the BSDish way (that Slackware does) and place them at the /etc/rc.d. Be a little creative and combine the two methods (a symbolic link or two is not that hard).

Is it inevitable that in Linux we should repeat the follies of the Unix-wars?

mlpa 12-06-2012 03:26 AM

Quote:

Originally Posted by rouvas (Post 4843652)
I still don't get it.
Why do you want to be distro-specific?

Why don't you follow the SysV way of placing your startup/shutdown scripts in /etc/rc.d/rc3.d (example for runlevel 3) and you have to follow somebody else's bright idea? Or you can follow the BSDish way (that Slackware does) and place them at the /etc/rc.d. Be a little creative and combine the two methods (a symbolic link or two is not that hard).

Is it inevitable that in Linux we should repeat the follies of the Unix-wars?

I personally agree with what you say. But the point is, in this department the winning distro is Debian/Ubuntu.
I can do whatever I want with my server, by the code, scripts and services have to compatible with the majority.

I cannot force the other people in the lab to adopt other schemes when they are happy with Debian/Ubuntu.

BTW I an using the Slackware way, but instead of having to write all the time the same command in bach I made a script. Like I said in the beginning service command is just to start stop a command from command line without change to /etc/rc.d/... and I am writing a insserv that will add a place the script in /etc/rc.d/ and start the script in rc.local and stop it in rc.local_shutdown.

It is still the Slackware way, but packed in two commands with the same name as present in Debian so the deployment is the same for every member of the lab.

rouvas 12-07-2012 01:37 AM

Well, if it scratches your itch...

BTW, I don't think that the "Slackware way" its the absolutely best method that we should engrave in stone and never deviate, I would be happy with almost any method provided it is standardised.

Anyway, thanks for sharing. Never a bad thing.

commandlinegamer 12-07-2012 04:28 AM

Personally I like djb's way of doing things with daemontools, though that may be a bit tangential to the OP's requirements.

mlpa 12-07-2012 10:41 AM

I created a git repository if anyone is interested, link.
I also created a insserv that adds a service to rc.local and rc.local_shutdown.

Like I said in the beginning this is not a fix for Slackware nor a solution for everyone but if someone like my is surrounded bey Debian people can be a way to normalize scripts.

I known that this can be a inglorious effort :)

jpollard 12-07-2012 10:49 AM

"service" is just a shell script... You can easily copy it.

mlpa 12-07-2012 12:00 PM

Quote:

Originally Posted by jpollard (Post 4844642)
"service" is just a shell script... You can easily copy it.

I actually made one. In mine the script search for the normal service name (openfire) and with rc. prefix (rc.openfire).
But I could check the "service" script to compare and improve mine.

Thanks

jpollard 12-07-2012 02:59 PM

The only reason the "service" script exists is to ensure the proper environment to run the startup script.

On Fedora it got to be a hassle as so much of the root user gets corrupted by gnome setups, that it needed something to clean out the cruft to get to the environment used during boot.


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