VPS in CentOS7, run Files JAVA on start my vps - linux

my vps in centos 7 ,
I have applications developed in java, 3 files .jar .
I need to run this files when my vps start or log-in, like example "java -jar file-name"
how i can run that file like service
i have the second question is,
what is the file in centos that has the list of services that run when you start centos.
For edit that file and add my jar. files

The second questions:
CentOS uses systemd to start system-wide or user-defined services. You can use systemctl to find out. For example, checking out the SSH server daemon, we can do:
[user1#centos Good]$ systemctl | grep ssh
sshd.service loaded active running OpenSSH server daemon
You can write your own .service file and put it under one of the following directories to make your java program run like a service.
/usr/lib/systemd/system/
/lib/systemd/system/
To know more about systemd and .service file, you can check CentOS / RHEL 7 : Beginners guide to systemd
edit: 2019-11-13 18:53:47
//java_program.service
[Unit]
Description=java_program
[Service]
Type=simple
User=root
ExecStart=/usr/bin/java -jar /root/folder/name.jar
RestartSec=5
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
[Install]
WantedBy=multi-user.target
Place java_program.service at path /usr/lib/systemd/system/java_program.service
Run command:
sudo systemctl enable java_program
sudo systemctl start java_program

Related

Bash script to run automatically after server restart

I have a script, it restarts tomcat in 2 minutes,, I would like that
this script was started constantly after server restart, I will be grateful if someone shows how to do it?
#! /bin/bash
sleep 120
systemctl restart tomcat
Read archwiki about systemd/Timer or maybe original freedesktop documentation about systemd.timer.
Create a file my_super_tomcat_restarter.timer in /etc/systemd/system/:
[Unit]
Description=Superbly restart my tomcat service every 120 seconds!
[Timer]
OnBootSec=120sec
OnUnitActiveSec=120sec
[Install]
WantedBy=timers.target
Create a file my_super_tomcat_restarter.service in /etc/systemd/system/ with the content:
[Unit]
Description=Superbly restart tomcat service!
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart tomcat
Execute from your terminal as root:
systemctl enable my_super_tomcat_restarter.timer
The timer should fire in 120 seconds and execute my_super_tomcat_restarter.service which in turn will restart your tomcat service.
either add it in init.d or systemd based on your Linux distribution of yours. in both cases you have to be root to add your script.

Auto-starting Twonky Server on Ubuntu 18.04 using systemd

I was trying to set up a Twonky Server on Ubuntu. The server works fine, but I could not get systemd to autostart the server (using a service file I created at /etc/systemd/system/twonkyserver.service). Sometimes I got the cryptic error message that some PID-file (/var/run/mediaserver.pid) is not accessible, the exit code of the service is 13, which apparently is a EACCES Permission denied error . The service runs as root.
I finally managed to fix the problem by setting PIDFile in the twonkyserver.service file to /var/run/mediaserver.pid. For reference, find the service file below:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
As described above, the below service file auto-starts the Twonky Server on boot. Simply create it using vim /etc/systemd/system/twonkyserver.service. This assumses that you have installed the Twonky Server to usr/local/twonky. The shell-file twonky.sh already provides a nice interface to the service file (twonky.sh start|stop|reload|restart, also see twonky.sh -h).
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
I would slightly amend the start and stop commands from twonky.sh and put them directly into the twonky.service file for systemd:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
#Systemd will ensure RuntimeDirectory for the PID file is created under /var/run
RuntimeDirectory=twonky
PIDFile=/var/run/twonky/mediaserver.pid
# use the -mspid argument for twonkystarter to put the pid file in the right place
ExecStart=/usr/local/twonky/twonkystarter -mspid /var/run/twonky/mediaserver.pid -inifile /usr/local/twonky/twonkyserver.ini -logfile /usr/local/twonky/twonky.log -appdata /usr/local/twonky
ExecStop=kill -s TERM $MAINPID
ExecStopPost=-killall -s TERM twonkystarter
ExecStopPost=-killall -s TERM twonky
# Twonky 8.5.1 doesn't reload, it stops instead (on arm at least)
# ExecReload=kill -s HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
You need to be sure the paths in the ExecStart command match where you unpacked twonky, and also where you want the .pid file, configuration, logfile and runtime appdataunless you are happy with their default locations.
After putting that all into/etc/systemd/system/twonky.server, run
sudo systemctl daemon-reload
sudo systemctl start twonky
sudo systemctl enable twonky

How to install Jenkins Ubuntu slave as a service?

I have a Ubuntu 16.04 LTS machine where I am successfully connected to the Jenkins server via JNLP connection. Steps I took for the connection are the following:
Create a directory in the slave called /home/MyUbuntu/Jenkins
Download both agent.jar and slave-agent.jnlp files into the
directory
Run this command from the terminal:
java -jar agent.jar -jnlpUrl http://my-jenkins-server:8080/computer/MyNode/slave-agent.jnlp -secret 6f8bb3250d6dbcda77979797997b0ea6bcaaa064785d558c0e4ea07d03 -workDir "/home/MyUbuntu/Jenkins"
The connection is successful.
Problem:
Once I close the terminal the connection gets disconnected.
Question:
How do I add this as a service in Ubuntu 16.04 LTS so whenever the machine is rebooted it starts as a startup???
If you use System D, add a file like this to /etc/systemd/system/.
[Unit]
Description=Jenkins slave connection
Wants=network.target
After=network.target
[Service]
ExecStart=java -jar agent.jar -jnlpUrl http://jenkinsurl:port/endpoint.jnlp -secret 4lph4num3r1cs3cr3t -workDir "/base/path/of/your/jenkinsjar"
Restart=always
WorkingDirectory=/base/path/of/your/jenkinsjar
User=my-user
Group=my-group
RestartSec=20s
[Install]
WantedBy=multi-user.target
Alias=jenkins.service
Permissions and ownership of the file may vary based on the service or OS. Long list files in /lib/systemd/system/ to get an idea of what perms you need or want (probably root:root 644).
Notice the command is the command that Jenkins provides for you when you create an agent jar. Just use that for ExecStart.
For user and group, I use the user that owns the directory where the Jenkins workspace is located. For example, if the Jenkins workspace is in /home/ubuntu, I specify ubuntu as user and group.
After that...
Prefix these with sudo if you're not running as root:
Probably a good idea to reload System D: systemctl daemon-reload.
Start: systemctl start jenkins.service. Notice this command pertains to the last line of the file Alias.
Enable it if you want the service to start with your computer: systemctl enable jenkins.service.
Here are the Git Gists I based my file on:
https://gist.github.com/unakatsuo/d4711f52a0ab0b9bc8010018149a7e84
https://gist.github.com/dragolabs/05dfe1c0899221ce51204dbfe7feecbb
I'm sure there's a lot more that can be done for the service config but in my case, I manage a lot of different servers and just need the thing to start automatically after boot!

Convert upstart script to systemd for django channel on ubuntu 16.04

I am following http://masnun.rocks/2016/11/02/deploying-django-channels-using-daphne this tutorial to deploy Django channel on Nginx.In this tutorial, they used upstart script to run daphne serve.I need to convert upstart script to systemd because i am working on ubuntu 16.04.
Below is upstart script
start on runlevel [2345]
stop on runlevel [016]
respawn
script
cd /home/ubuntu/Project/projectname
export DJANGO_SETTINGS_MODULE="projectname.settings"
exec daphne -b 0.0.0.0 -p 8001 projectname.asgi:channel_layer
end script
Below is systemd script which i converted
[Unit]
Description=daphne server script
[Service]
Environment=DJANGO_SETTINGS_MODULE="projectname.settings"
WorkingDirectory=/home/ubuntu/Project/projectname
ExecStart=daphne -b 0.0.0.0 -p 8001 projectname.asgi:channel_layer
Restart=always
[Install]
WantedBy=multi-user.target
When i run systemd service status it gave me below error
Failed to get properties: No such interface ''
From man systemd.service in the section on ExecStart=:
For each of the specified commands, the first argument must be an absolute path to an executable.
You have other problems, but first you need to provide a full path to daphne. You can check your file with:
systemd-analyze verify /path/to/your/file.service

OrientDB as a daemon on Ubuntu not stopping

Followed the documentation for Installing orientdb on Linux/Ubuntu on the following location, http://orientdb.com/docs/2.1/Unix-Service.html
Set the following variables : JAVA_HOME, JRE_HOME, ORIENTDB_HOME and the following changes in the below files,
orientdb.sh
#!/bin/sh
# OrientDB service script
#
# Copyright (c) Orient Technologies LTD (http://www.orientechnologies.com)
# chkconfig: 2345 20 80
# description: OrientDb init script
# processname: orientdb.sh
# You have to SET the OrientDB installation directory here
ORIENTDB_DIR="/local/some-location"
ORIENTDB_USER="some-user"
Installing for systemmd
/etc/systemd/system/orientdb.service
[Unit]
Description=OrientDB Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
ExecStart=/local/some-location/bin/orientdb.sh start
ExecStop=/local/some-location/bin/orientdb.sh stop
ExecStatus=/local/some-location/bin/orientdb.sh status
It is possible to start the service by the following command
systemctl start orientdb.service
But the service will not stop after giving the following command
systemctl stop orientdb.service
Machine Info :
Java 8.0_31
Orientdb 2.1.12
Ubuntu 15.04 on Virtual Box VM
modify your service file according to this:
$ cat /etc/systemd/system/orientdb.service
[Unit]
Description=OrientDB Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=<USER>
Group=<GROUP>
ExecStart=<ORIENTDB_INSTALL_PATH>/bin/server.sh
SuccessExitStatus=143
modifying with your user,group and path.
than reload the service systemctl daemon-reload
Using this is working for me on Fedora23.
Let me know.
Ivan
EDIT
Tried on Ubuntu 15.04 and it works.
There is an issue with shutdown on 2.1.x See: Joe Taras comment. You should update to the latest 2.2.x version.

Resources