Celeb Glow
general | March 18, 2026

How to start a delay job correctly using Upstart?

The current script:

start on (net-device-up and local-filesystems and runlevel [2345] and started rsyslog)
stop on runlevel [!2345]
respawn
exec /usr/sbin/openvpn --config /etc/openvpn/devops/config

The problem is if it failed to start for some reasons:

Sep 24 01:55:07 p vpn-devops[5075]: Cannot load certificate file /etc/openvpn/devops/server.crt: error:02001002:system library:fopen:No such file or directory
: error:20074002:BIO routines:FILE_CTRL:system lib: error:140AD002:SSL routines:SSL_CTX_use_certificate_file:system lib
Sep 24 01:55:07 p vpn-devops[5075]: Exiting

Upstart still think that it is started and exit with 0 status immediately.

My first try:

post-start script for try in $(seq 9); do if [ nc -q0 -zu localhost 1194 ]; then exit 0 fi sleep 1 done exit 1
end script

I thought that it will work but actually it's not. The reason is exit code is ignored in the post-start.

My second try:

expect fork
respawn
pre-start script /usr/sbin/openvpn --config /etc/openvpn/devops/config if [ $? -ne 0 ]; then exit 1 fi
end script
script while kill -0 $(cat /var/run/openvpn/devops.pid); do sleep 1 done
end script

Now the exit status is correct in case of failure:

start openvpn-devops
start: Job failed to start
echo $?
1

but start hangs (does not return the shell prompt) in normal case although the pid is tracking correctly:

status openvpn-devops
openvpn-devops start/pre-start, process 7565
cat /var/run/openvpn/devops.pid
7565

The same thing happened if I use:

expect daemon
/usr/sbin/openvpn --config /etc/openvpn/devops/config --daemon

My third try:

expect fork
exec /usr/sbin/openvpn --config /etc/openvpn/devops/config --daemon

then start worked but it track the wrong pid because the number of forks is not correct:

start openvpn-devops
openvpn-devops start/running, process 7720
status openvpn-devops
openvpn-devops start/running, process 7720
cat /var/run/openvpn/devops.pid
7715
ps -ef | grep vpn
nobody 7715 1 0 02:41 ? 00:00:00 /usr/sbin/openvpn --config /etc/openvpn/devops/config --daemon

What did I do wrong?

1 Answer

The current script is correct, it does what it should do.

Second script hangs forever because /usr/sbin/openvpn --config /etc/openvpn/devops/config never exits, it runs in foreground.

Third script also hangs but because openvpn --daemon only forks once.

expect daemon /usr/sbin/openvpn --config /etc/openvpn/devops/config --daemon

Changed it to expect fork will work if config file is correct. If config file is wrong, openvpn won't fork, so it won't work correctly.

TL;DR: just stick with current script.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy