Results 1 to 5 of 5

Thread: Run script as a daemon under root?

  1. #1

    Default Run script as a daemon under root?

    How do I turn a Python script into a daemon that runs under root, and starts automatically when I boot into Linux?

    /home/username/bin/pystromo/pystromo-remap.py -m /home/username/.config/pystromo/default.map

    Failing that, how do I start the script as a background process from the console? I thought 'bg' would do that, but it only takes a pid from an already-running process as an argument.

    Basically I want this remapper running constantly in the background, preferably as a service. Unfortunately it currently only works when run as su, which I'm trying to fix.

    Thanks!

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,243

    Default Re: Run script as a daemon under root?

    In /etc/init.d//skeleton you will find a startpoint for a start/stop script.

    Copy this to a new file in/etc/init.d and adept. Do not forget to change the lines with ## at the beginning, because they are important.

    After you did this correct, you can use Yast > System > System services (Runlevel) to start the service. YaST will that also do all what is needed to start/stop it at reaching the correct runlevel(s).

    Inside the start part of the script I would call your Python scriupt with full path and with a & at the end. Like:
    Code:
    /home/username/bin/pystromo/pystromo-remap.py -m /home/username/.config/pystromo/default.map &
    My advice would be to put the script NOT somewhere in the home directory of a end user, but in a place more dedicated to root like /usr/local/bin.
    Always make a clear divide between root and the user, even that user is one you trust likle yoursekve.

    Consider what you must have in the stop part of the script. Ideally you should there kill your deamon in a neat way. Fetching the PID of the Python script (which then should be saved in a file by the Python script) and use the kill statment might be an option here.
    Henk van Velden

  3. #3

    Default Re: Run script as a daemon under root?

    Thanks hcw. I looked at the skeleton file, and i'm afraid that correctly modifying it appears to be beyond my ability at the moment.

    Starting the script manually and then moving it to the background would be quite sufficient. Normally I would do that with the 'bg' command, but it does not seem to work on Suse. Is there a Suse version of it?

    Thanks!

  4. #4
    Join Date
    Nov 2009
    Location
    West Virginia Sector 13
    Posts
    15,760

    Default Re: Run script as a daemon under root?

    The ampersand & is used to start a process as a new process ie not a child so the starting process can move on and even terminate without terminating the new process.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,243

    Default Re: Run script as a daemon under root?

    When you start a process from the shell and you want to detach it such that it runs independent (the shell not waiting until it ends, but returning with a prompt immediatly you use the $ at then end of the statment (like my example earlier). You can then exit the shell if youy like. But take care, the detached process still has connections by it's stand input and standard output to the termminal where your shell is/was running. So better redirect it like this general case:
    Code:
    process --options several parameters <input >output 2>erroroutput &
    See
    Code:
    man bash
    the paragraph SHELL GRAMMAR, the sub-paragrapf Lists.

    @gogalthorp
    The detached process is still a child from the shell where you executed it until the moment you finish that shell. It then becomes a child of the init process (1).
    Henk van Velden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •