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.

- 93,103
- 40
- 240
- 233

- 139
3 Answers
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 07 '18 at 19:49
-
-
1PSA: 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 thechkconfig
tool, mentioned in M4rty's answer and further explained here: https://geekflare.com/how-to-auto-start-services-on-boot-in-linux/ – Justin Morgan Oct 09 '18 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... – Oct 10 '18 at 10:46
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

- 1,153
-
Link to documentation for using
chkconfig
: https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-services-chkconfig.html – AlikElzin-kilaka Jan 02 '18 at 13:32
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

- 477
systemd
– Jun 01 '17 at 10:33