user526206
user526206

Reputation:

How to run a script as a service in UBUNTU

I have a script which normally i run using ./myscript.sh(contain java run command) on linux. Now i want to make it as a service so it run automatically after machine restart and if i want to stop and start again simply find the process and kill and start it again from command line.

What i find with quick google search is to place the script in /etc/init.d directory but confusing with one thing that command inside this script using other certificate files which i normally place on same level where this script is place. Do i need to move all others file along with this script under /etc/init.d or is there any better way that i simply mention the path of this script in some file?

Upvotes: 6

Views: 34674

Answers (1)

agilob
agilob

Reputation: 6243

You need to write systemd service file.

Simplest script looks like this:

[Unit]
Description=Virtual Distributed Ethernet

[Service]
ExecStart=/usr/bin/YOUR_SCRIPT

[Install]
WantedBy=multi-user.target

Also you need: systemctl daemon-reload after creating new service.

Upvotes: 17

Related Questions