How to install Jenkins Ubuntu slave as a service? - linux

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!

Related

Nitrogen through systemd services doesn't work

I am writing a tool (zxcV32/OpenRWC) that fetches wallpapers from Reddit and sets them to the monitor(s) using nitrogen.
To make it easy to install and run automatically, I have created a deb package and a systemd service.
When the service is started using
sudo systemctl start openrwc#$USER.service
nitrogen errors out with exit status 1. (No other error message). And yes, the exec installed by the deb package works fine when manually run from the terminal.
I have compared that the command run by the tool is precisely the same when run through the systemd service or terminal run.
Sample command
nitrogen --set-scaled /home/zxcv32/.config/OpenRWC/fz41kmzk1wj91.jpg --head=0
Service
[Unit]
Description=Reddit Wallpaper Changer for GNU/Linux
Requires=display-manager.service
After=display-manager.service
StartLimitIntervalSec=0
[Service]
Type=simple
ExecStart=/usr/bin/openrwc
Restart=always
RestartSec=5
User=%i
[Install]
WantedBy=graphical.target
What may be wrong with the service? I want the service to be the user's choice, if they want to run it or not.
BTW I found this question that claims that nitrogen works through a service. (maybe there is a difference between running nitrogen directly through system service and through a go funciton)
System: Debian 11 5.10.0-17-amd64
Found the issue.
Systemd does not have access to certain environment variables.
DISPLAY environment variable needs to be set in the openrwc#.service.
[Service]
Environment="DISPLAY=:0"

VPS in CentOS7, run Files JAVA on start my vps

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

Restarting Services in Linux after a Server Reboot

So today one of our application servers were restarted due to some issue and after restart we found that our application services were not running.
I want to create one script which will check these below services after a server restart and start them automatically if found stopped:
1st Service with Path : /opt/bea/config/nm/nm-sdi-abc/beaNMctl.sh 
2nd service TOMCAT - Path : /opt/apache/tomcat/bin --- Service name startup.sh
Catch here is 1st service can be started with the normal id account that i use.
But 2nd service can be restarted after logging into a different service account on same server and network. Like below:
[x201691#abc bin]$ su - apache
Password:
-bash-2.05b$ cd /
-bash-2.05b$ cd /opt/apache/tomcat/bin/
-bash-2.05b$ ./startup.sh
Can someone help?
Also we are not root users.
You can write a shell script:
echo YOUR_PASSWORD | sudo -S su
cd /opt/apache/tomcat/bin/
./startup.sh
Save this as a file somewhere you have access and add the following cron entry:
#reboot MYPATH/myscript.sh >> MYPATH/script.log 2>&1
script.log will contain any output or errors from your script. You can add date command to the script to help with information on when it was run. More information on cron here.
Also, if you have concern with putting password in the script, you can go through the discussion here.
Preferred approach when installing Tomcat in Linux is to make Tomcat as a service.
This will ensure your service is started after reboot
1. Create the service file with the following command:
touch /etc/systemd/system/tomcat.service
2. Assign the relevant rights to the file you created:

 chmod 664 /etc/systemd/system/tomcat.service
3. Paste the following content in the file while adapting it to your configuration:
[Unit]
Description=Application description/name
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
ExecStart=$CATALINA_HOME/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
Install]
WantedBy=multi-user.target
4. Reload the service daemon:
 systemctl daemon-reload
5. Start the service:
 systemctl start tomcat
6. To check status : 
 systemctl status tomcat

How to run node js even on server restart

I built a Nodejs project and now it runs smoothly.
I use forever service for running file in background but if server get restarted
the daemon won't be started automatically and should be started manually.
I want to run the daemon even the server get rebooted
You could add the forever command in .bash_profile so that every time the server restart, your command will simply be also executed.
nano ~/.bash_profile
forever start app.js # add this command to the file, or whatever command you are using.
source ~/.bash_profile # very important, else changes will not take effect
Next time, on your server restart, your command will also run, hence creating a daemon of your node script.
Note: This is maybe not the best solution, but the one I have got.
Update
As #dlmeetei, suggested, you can also start your nodejs app like a service so that we can use the features given by a linux service.
First create a file in /etc/systemd/system, like:
touch /etc/systemd/system/[your-app-name].service
nano /etc/systemd/system/[your-app-name].service
Then, add and edit the following script according to your relevance.
[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first
[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
# WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target
Enable the service, it will marks the service for starting up on boot.
systemctl enable [your-app-name].service
Manage the service
systemctl start [your-app-name].service
systemctl stop [your-app-name].service
systemctl status [your-app-name].service # ensure your app is running
systemctl restart [your-app-name].service
Reference: https://www.axllent.org/docs/view/nodejs-service-with-systemd/
Thanks #dlmeetei for sharing the link.

How to run an app as a daemon with systemd?

I'd like to run syncthing as a daemon, trying to follow this hint here from the syncthing manual.
I'm running on Fedora 25 and syncthing 0.14.24.
The executable is pointed to via a symlink in /usr/bin/syncthing which can be executed by any user (tested this successfully).
To enable the service, I'm typing (myuser is replaced with my actual username in all of the below):
sudo systemctl enable sycnthing#myuser.service
Which returns:
Failed to lookup unit file state: Invalid argument
I don't understand what the error message means. How could I get to run syncthing as a daemon?
syncthing#myuser.service:
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %I
Documentation=man:syncthing(1)
After=network.target
Wants=syncthing-inotify#myuser.service # I also commented this line out; didn't have an effect
[Service]
User=%i
ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0
Restart=on-failure
SuccessExitStatus=3 4
RestartForceExitStatus=3 4
[Install]
WantedBy=multi-user.target
I think myuser should be substituted for your actual username.
Arch wiki has a pretty good article:
System service
Running Syncthing as a system service ensures that it is running at startup even if the user has no active session, it is intended to be used on a server.
Enable and start the syncthing#myuser.service where myuser is the actual name of your user.
Credit: https://wiki.archlinux.org/index.php/Syncthing

Resources