I'm using Amazon Linux. I have a script to start and stop a service, written in bash, located at


At the various run levels, I have symlinks to ensure the script starts and stops, for instance


But my question is, what do I need to do to ensure if the service fails, it can automatically restart? I read on CentOS, you can create a file (e.g. "wildfly.service") with the directives


Where do the equivalent directives live on Amazon Linux?


Amazon Linux 2

If your version of Amazon Linux is >=2.0, it has systemd by default. In this case, you should simply be able to use the same unit file you have been using on CentOS, with the restart directives.

Amazon Linux AMI

If you are you are running Amazon Linux AMI, you will need to either use a separate supervisor to monitor your process (as poige mentioned), or utilize /etc/inittab.

For example, in order to have sysvinit automatically restart your process, add the following to /etc/inittab:

# Start and respawn process
mydaemon:2345:respawn:/path/to/executable argument1 argument2

This tells sysvinit to start the process on runlevels 2, 3, 4, and 5 and restart it when it terminates.

Also, if you wish to add some commands around the executable to be run on startup, the executable path can instead refer to a script that runs the program in the foreground.

  • Hi, I am running AMI so it looks like the /etc/inittab is the way to go. Couple of questions -- is "mydaemon" an arbitrary string I provide? Also, I normally start my process with "sudo service wildfly start", which launches the bash script "/etc/init.d/wildfly" with a "start" argument, so would the rest of the directive look like "etc/init.d/wildfly start"? – Dave Jan 7 at 3:51
  • mydaemon is an arbitrary string. See man inittab for details. The existing init script likely makes the wildfly daemon fork into the background, so you would have to modify it so the daemon runs in the foreground for it to work in inittab. As an alternative to modifying, you could just directly specify the path to the wildfly executable directly (or 'interpreter wildfly' if it depends on a runtime) with the arguments listed after it. However, this may remove wanted functionality that was formerly provided in the init script. – novice Jan 7 at 4:28
  • If you still needed functionality provided in the init script, perhaps you may be able to share the script so we could give you pointers could be given on how to modify it so that it does not fork the daemon. – novice Jan 7 at 4:29
  • After looking at the package sources of wildfly, it seems that the default systemd unit simply calls a series of wrapper scripts that already do the restarting for you, doing away with the dependency on systemd's restarting mechanism. If you look at the unit, you may be able to create a sysvinit init script from these wrapper scripts. Specifically, such script would be a one-shot start, first setting up the environment, making sure that LAUNCH_JBOSS_IN_BACKGROUND is an empty value, and then running /usr/share/wildfly/bin/launch.sh as user wildfly with the required arguments. – novice Jan 7 at 5:02
  • The above will eventually either call /usr/share/bin/wildfly/domain.sh or /usr/share/wildfly/bin/standalone.sh, which will auto-restart the jboss instance due to LAUNCH_JBOSS_IN_BACKGROUND being unset. – novice Jan 7 at 5:28

I'm using Amazon Linux.

ok, type in man init

If it's any fresh you'll see it's systemd in fact. So thus your q-n transforms into something answered already.

In case you consider systemd over-engineered (as many of us in fact do) you can give a try to venerable daemon-tools or smth alike (runit, supervisord). Be prepared it might be not in standard repos Amazon Linux comes with though.

  • 1
    Hi, So you're suggesting the answer from the post you linked to? When I tried running "sudo systemctl edit wildfly.service", I got the error, "sudo: systemctl: command not found". – Dave Jan 3 at 23:04
  • I suggested first man init – poige Jan 4 at 0:16

Your Answer

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

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