Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

-- EDIT, still some issues --

OK my script uses a loop to wait for network connections. So when I run it, even with daemon it will just sit there and not take me back to the shell. I tried su -c "/home/webreports/report-list &" USER but it tried to run as the user & even though i have it in quotes, i even tried single quotes.

-- Original -- I have made a script (yet to be tested) for running a bash script as a service. I have two questions.

1) how do i get it to run as a specific user? the software we use CANNOT be run as root and will fail horribly if it does (horrible software we are sadly stuck with). So how do i make it run the service as user "JOEBOB" lets say.

2) Do I just put the script file into "/etc/rc5.d " to be able to use "service report-listen start" ?

--- Script --

#!/bin/sh
#
# myservice     This shell script takes care of starting and stopping
#               the /home/webreports/report-listen
#

# Source function library
. /etc/rc.d/init.d/functions


# Do preliminary checks here, if any
#### START of preliminary checks #########


##### END of preliminary checks #######


# Handle manual control parameters like start, stop, status, restart, etc.

case "$1" in
  start)
    # Start daemons.

    echo -n $"Starting report-listen daemon: "
    echo
    daemon /home/webreports/report-listen
    echo
    ;;

  stop)
    # Stop daemons.
    echo -n $"Shutting down report-listen: "
    killproc /home/webreports/report-listen
    echo

    # Do clean-up works here like removing pid files from /var/run, etc.
    ;;
  status)
    status /home/webreports/report-listen

    ;;
  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0
share|improve this question
    
I think not being able to run as root is a feature :)! –  Zypher Jan 7 '10 at 18:35
    
It would be except it will let you run as root and then change all the permissions and lock everyone out, then crash. –  The Digital Ninja Jan 7 '10 at 18:37
    
What distribution? –  Dennis Williamson Jan 7 '10 at 18:43
    
Redhat enterprise –  The Digital Ninja Jan 7 '10 at 18:49
add comment

5 Answers

up vote 1 down vote accepted

Use su to run the script as a different user:

daemon su -c /home/webreports/report-listen johndoe

where johndoe is the user you want it to run as.

Put the script in /etc/init.d/myservice, then symlink it to /etc/rc.d/S99myservice.

share|improve this answer
    
Only issue I have is that it doesn't seem to be running the script in the background. I tried daemon su -c '/home/webreports/report-listen &' USER but then it tried to run as the user & even though I have it in quotes –  The Digital Ninja Jan 7 '10 at 17:24
add comment

just copying will not do the job. You have to take care the program is decoupled from stdin and stdout. Therefor all output has to be printed to a logfile. You also have to background the program, which should be done by the daemon function.

share|improve this answer
add comment

You haven't mentioned what distribution you're using, but it seems like you're using a Red Hat based one. I'll base my answer on that assumption.

To answer the first part of your question, about running the script on startup. You have to put the script in /etc/rc.d/init.d, and add a "chkconfig" compatible line to allow you to add it to the correct levels with the "chkconfig" command. see a previous question on that issue.

As for starting the script with a specific user. Since you're already using the "daemon" function, you can try using the "--user" switch:

daemon --user=reportsuser /home/webreports/report-listen
share|improve this answer
add comment

If you're using Ubuntu (Debian based OS) you can base your script on /etc/init.d/skeleton and replace DAMEON var:

DAEMON=su -c "/home/webreports/report-listen" joebob

You will have a pid file also for clean start/stop.

You can start it automatically during boot with (Assuming your service script is named /etc/init.d/my-report-listen):

$ sudo update-rc.d my-report-listen defaults

This method is just an attempt, not a recipie that works. I hope it helps

share|improve this answer
add comment

Daemonising code is hard to get right. For this, I use daemontools -- basically it does the hard work of running in the background, and you just write your script as though you're going to run it in the foreground, and then tell daemontools to run it instead.

See http://cr.yp.to/daemontools.html for all the documentatory awesome.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.