When I boot my CentOS box, the httpd service starts automatically. How do I make a custom service that does the same thing? I have a program I use for mining, and I don't want to need to run ./miner every time I boot the machine.

migrated from unix.meta.stackexchange.com May 31 '17 at 17:57

This question came from our discussion, support, and feature requests site for users of Linux, FreeBSD and other Un*x-like operating systems.

  • what version of CentOS are you using? – nwildner May 31 '17 at 18:06
  • I'm using CentOs 7.1 server. – Rei Jun 1 '17 at 3:07
  • Are you sure that your miner program doesn't already have a script in /etc/init.d/ ? – M4rty Jun 1 '17 at 9:46
  • Centos 7.1 -> systemd – nwildner Jun 1 '17 at 10:33
up vote 3 down vote accepted

Since you are using CentOS 7.x, create a Unit. vim /usr/lib/systemd/system/miner.service as root and put the following contents:

[Unit]
Description=miner

[Service]
ExecStart=/path/to/miner

[Install]
WantedBy=multi-user.target

You could add ExecStop= and ExecReload= options if there are specific arguments used to close or reload services.

After that, you just need to systemctl enable miner.service to make it start on each boot.

Related Stuff:

  • On my CentOS 7 box, I got a "command not found" when running "systemd enable miner.service". So I had to run "systemctl enable miner.service" instead. – Dave May 7 at 19:49
  • systemctl. My fault. Fixing it :) – nwildner May 7 at 20:19
  • 1
    PSA: If, like me, you Googled your way here looking for how to do this in CentOS 6, you'll notice you don't have the systemd folder. What you want is the chkconfig tool, mentioned in M4rty's answer and further explained here: geekflare.com/how-to-auto-start-services-on-boot-in-linux – Justin Morgan Oct 9 at 17:41
  • @JustinMorgan, that's right. CentOS 6 does not use systemd as init or service manager system. One quick hack to it would be start this software using the /etc/rc.local script instead of creating a service start script... – nwildner Oct 10 at 10:46

If you just need to run the command on boot, trying to make an actual service isn't really required. The simplest thing to do is to drop . /path/to/miner in /etc/rc.d/rc.local. This file is a script that is run on every boot (and make sure rc.local is executable), so your command will run when the server starts up.

If you need it to actually be handled as a service though, the best way to do that will depend on whether you're using CentOS 7 or 5/6.

edit: forgot to mention, this will be executed as root, so if you need it run as a non-root user, use instead su - username -c /path/to/miner

Depending on the miner program provider you might have the associated service already declared.

On centOS you can check :

# chkconfig --list 

and if you see your program you can tell the system to run it automaticly at boot time

# chkconfig postgresql on

If you don't find any result you can create your own dummy script using a template for example :

How do I create a service for a shell script so I can start and stop it like a daemon?

then put it in /etc/init.d/ and chmod +x it

You should be able to manage you miner application as a service with all the advantages that comes with it

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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