Coder Social home page Coder Social logo

init-script-template's Introduction

System V init script template

A simple template for init scripts that provide the start, stop, restart and status commands.

Handy for Node.js apps and everything else that runs itself.

Getting started

Copy template to /etc/init.d and rename it to something meaningful. Then edit the script and enter that name after Provides: (between ### BEGIN INIT INFO and ### END INIT INFO).

Now set the following three variables in the script:

dir

The working directory of your process.

cmd

The command line to start the process.

user

The user that should execute the command (optional). If not set, the command will be called as root (via sudo ...).

Here's an example for an app called algorithms:

dir="/var/apps/algorithms"
cmd="node server.js"
user="node"

Script usage

Start

Starts the app.

/etc/init.d/algorithms start

Stop

Stops the app.

/etc/init.d/algorithms stop

Restart

Restarts the app.

/etc/init.d/algorithms restart

Status

Tells you whether the app is running. Exits with 0 if it is and 1 otherwise.

/etc/init.d/algorithms status

Logging

By default, standard output goes to /var/log/scriptname.log and error output to /var/log/scriptname.err. If you're not happy with that, change the variables stdout_log and stderr_log.

License

Copyright (C) 2012-2014 Felix H. Dahlke

This is open source software, licensed under the MIT License. See the file LICENSE for details.

init-script-template's People

Contributors

alpacagh avatar azadam avatar fhd avatar ghallsimpsons avatar kzar avatar loisaidasam avatar victron avatar zanedeg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

init-script-template's Issues

stop for loop

I think in Bourne shell the loop for i in {1..10} is not working. At least for me it only goes through once and causes the stop to not wait long enough. Other scripts I looked at tend to use other type of loops.

Cannot stop script without sudo

I am having a problem stopping a script that I am running using the user option. I configured the permissions on some necessary directories to be able to start the service properly, but when I try to stop it, I get the following message:

kill: Operation not permitted

After some investigation I found that the PID stored for the init script is the one for the sudo -u <user> <cmd> command, and there is a separate one for the actual running script. I started the service and I cannot kill the process using kill <pid> using both the pid for the sudo -u command (stored in /var/run/<cmd>.pid) as the desired user. Any ideas?

License

Could you add a license to the repo? I'd like to reuse your code, but need it to either be a FOSS license or public domain.

Thanks!

Brace expansion {1..10} in for loop is not portable

Bash, even in posix emulation mode, will expand braced sequences (I don't think that's the official name for this construction).

$ bash --posix -c 'echo {1..10}'
1 2 3 4 5 6 7 8 9 10

But a more minimal shell like dash will not perform this expansion

$ dash -c 'echo {1..10}'
{1..10}

seq is pretty common on Linux, but is not itself POSIX. It isn't available on FreeBSD, for instance.

for i in `seq 1 10`; do
    echo "$i"
done

The POSIXest way I can think of to do this is to use a while loop and an explicit test.

i=1
while [ "$i" -le 10 ]; do
    echo "$i"
    i="$((i+1))"
done

Name

Shouldn't it be better to assign the value of the name variable manually instead of receiving the name of the script???. Because in Ubuntu the first time the script is runned by the system it appends the prefix S/Kxx to the pid file, causing commands start, stop and status to fail, it took me a while to figure out what was going on with the script.

name=basename $0

name="appname"

Thanks for your template, it is such a help for newbies. (y)

Service won't start since pid file is found

OS: Ubuntu 22.04.1 LTS

I used the template to create a service for starting the docker daemon.

Starting the service:

$ sudo /etc/init.d/docker start
Starting docker

Querying status:

$ sudo /etc/init.d/docker status
Stopped

Surely enough, the docker daemon isn't running at this stage.

Checking the error log /var/log/docker.err I see

time="2023-05-05T12:31:24.062889666Z" level=info msg="Starting up"
failed to start daemon: pid file found, ensure docker is not running or delete /var/run/docker.pid

Deleting the docker.pid file and starting the service again yields the same result.

If I delete the line

echo $! > "$pid_file"

from the script, the service works as expected and the docker.pid is automatically created with the correct PID.

It appears that the System V init script isn't supposed to be writing the docker.pid file, but should be leaving that to init.

sudo: sorry, you must have a tty to run sudo

sudo: sorry, you must have a tty to run sudo

This message is found it error log file. This could be due to requiretty being enabled in visudo by default. Reference here.

If this is the case, it is better to apply workaround in the init script, instead of altering visudo default configuration.

Permission denied on log files without sudo

I am writing my first autostartup init.d script with a fresh instance of ubuntu amazon EC2 and I can't run it without sudo .

ubuntu@ip-172-31-XX-XXX:~$ /etc/init.d/codebox start                                                                                                    
Starting codebox
/etc/init.d/codebox: 44: /etc/init.d/codebox: nvm: not found
/etc/init.d/codebox: 50: /etc/init.d/codebox: cannot create /var/run/codebox.pid: Permission denied
/etc/init.d/codebox: 46: /etc/init.d/codebox: cannot create /var/log/codebox.log: Permission denied
Unable to start, see /var/log/codebox.log and /var/log/codebox.err 

If I run it with sudo it ill work but I don't konw how safe it is.
(Side question. If i edit the script and add just after cd "$dir" nvm use 0.12.7 nvm command is not found. Are there restrictions in what you can put inside an init.d script )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.