Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Custom OpenRC script not starting at boot
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
r7l
Tux's lil' helper
Tux's lil' helper


Joined: 16 Feb 2019
Posts: 90

PostPosted: Wed Mar 27, 2019 6:22 pm    Post subject: [SOLVED] Custom OpenRC script not starting at boot Reply with quote

Hello,

I wrote a custom OpenRC script to start a docker container. I saw others using systemd scripts for it and thought it might be possible to do the same with OpenRC as i don't want to install systemd. The script is running perfectly when started or stopped manually. But when set to start at boot time (in default runlevel, not boot) it does not work. It never starts automatically.

It's my first OpenRC script and i might have missed something:

Code:
#!/sbin/openrc-run

INSTANCE_NAME="${SVCNAME#*.}"

description="Portainer Stack Manager"

depend() {
    need docker net
}

checkconfig() {
    if ! grep -q "docker-compose" /var/lib/portage/world; then
        eerror "Docker Compose needs to be installed"
        exit 1
    fi
}

start() {
    checkconfig || return 1
    ebegin "Starting ${SVCNAME}"
    docker-stack-start portainer
    eend $?
}

stop() {
    if [ "${RC_CMD}" = "restart" ]
    then
        checkconfig || return 1
    fi

    ebegin "Stopping ${SVCNAME}"
    docker-stack-stop portainer
    eend $?
}


The part with docker-stack-start portainer or docker-stack-stop portainer is just a simple Bash script that starts or stops certain predefined Docker Stacks using Docker-Compose by putting a dynamically modified docker-compose.yml file into a certain directory and starting it. This is 100% working as i am able to run /etc/init.d/portainer start without any issues.

I don't see any kind of error messages in rc.log or any other output.

Can anyone help me?


Last edited by r7l on Fri Mar 29, 2019 10:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
geki
Advocate
Advocate


Joined: 13 May 2004
Posts: 2387
Location: Germania

PostPosted: Wed Mar 27, 2019 8:44 pm    Post subject: Reply with quote

On a quick glance, checkconfig has no return but exits?! better return not exit. And you better have full path to script best put in /usr/local/bin. Here is a basic sample of my selfmade openrc init script with start-stop-daemon and handle sigterm/hup/int and ignore sigpipe in daemonized script:

/etc/conf.d/wamp_server:
# /etc/conf.d/wamp_server

wamp_server_user="nobody"
wamp_server_group="nobody"

# Options to pass to daemon at startup/stop
wamp_server_opts=""

/etc/init.d/wamp_server:
#!/sbin/openrc-run

description="wamp server"

depend() {
        need mysql
}

start() {
        ebegin "Starting ${description}"
        start-stop-daemon --start --quiet --background \
                --user ${wamp_server_user:-nobody} \
                --group ${wamp_server_group:-nobody} \
                --exec /usr/local/bin/wamp_server \
                --pidfile /var/run/wamp_server.pid \
                --make-pidfile \
                -- ${wamp_server_opts}
        eend $?
}

stop() {
        ebegin "Stopping ${description}"
        start-stop-daemon --stop \
                --exec /usr/local/bin/wamp_server \
                --pidfile /var/run/wamp_server.pid \
                --retry SIGTERM/5
        eend $?
}

_________________
hear hear
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22657

PostPosted: Thu Mar 28, 2019 1:32 am    Post subject: Re: Custom OpenRC script not starting at boot Reply with quote

r7l wrote:
Code:
checkconfig() {
    if ! grep -q "docker-compose" /var/lib/portage/world; then
Does this string need to be in this file for your program to work? Is it possible that docker-compose could be installed and functional without being mentioned in this file?
geki wrote:
On a quick glance, checkconfig has no return but exits?! better return not exit.
It looks odd, but falling off the end will return success in this case.
Back to top
View user's profile Send private message
r7l
Tux's lil' helper
Tux's lil' helper


Joined: 16 Feb 2019
Posts: 90

PostPosted: Fri Mar 29, 2019 5:15 pm    Post subject: Reply with quote

I've just changed checkconfig() and added proper return statements.

Code:
checkconfig() {
    if ! grep -q "docker-compose" /var/lib/portage/world; then


This is basically just to protect the entire thing from myself. Docker Compose is needed to run this as it will just fail with errors within the called bash script. I am planning to use this script for every machine i am running with Docker and Portainer.

Is it really required to use start-stop-daemon? I've tried it but it does not create a PID file and fails when trying to stop it because of the missing PID file. What is the difference (code wise) between running it manually and on startup? It works just fine manually.

Thanks for your help!
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 31269
Location: here

PostPosted: Fri Mar 29, 2019 5:41 pm    Post subject: Reply with quote

r7l wrote:
I've just changed checkconfig() and added proper return statements.

Code:
checkconfig() {
    if ! grep -q "docker-compose" /var/lib/portage/world; then


This is basically just to protect the entire thing from myself. Docker Compose is needed to run this as it will just fail with errors within the called bash script. I am planning to use this script for every machine i am running with Docker and Portainer.

Yes but if someone put docker-compose in a sets the package will not be present in /var/lib/portage/world. Maybe is better check if /usr/bin/docker-compose file exists
_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
r7l
Tux's lil' helper
Tux's lil' helper


Joined: 16 Feb 2019
Posts: 90

PostPosted: Fri Mar 29, 2019 8:47 pm    Post subject: Reply with quote

fedeliallalinea wrote:
r7l wrote:
I've just changed checkconfig() and added proper return statements.

Code:
checkconfig() {
    if ! grep -q "docker-compose" /var/lib/portage/world; then


This is basically just to protect the entire thing from myself. Docker Compose is needed to run this as it will just fail with errors within the called bash script. I am planning to use this script for every machine i am running with Docker and Portainer.

Yes but if someone put docker-compose in a sets the package will not be present in /var/lib/portage/world. Maybe is better check if /usr/bin/docker-compose file exists


That might be true. I am not planning to release this script. It is basically for me starting containers with OpenRC instead of using SystemD. I know there are a few ways of doing this with Docker alone but i've found that to be unstable at times as well. I thought a start up script might do the trick. Especially on larger stacks. But it does not seem to be as easy as i thought.

So far i am still puzzled why it does work as expected when used with /etc/ini.d/portainer start but does not when starting up the system.
Back to top
View user's profile Send private message
r7l
Tux's lil' helper
Tux's lil' helper


Joined: 16 Feb 2019
Posts: 90

PostPosted: Fri Mar 29, 2019 10:24 pm    Post subject: Reply with quote

I've figured it out. The issue seems i was using a symlink for the file instead of having it in the directory. It works now. Thanks allot for everyone who helped me.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum