Restarting Services in Linux after a Server Reboot - linux

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

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.

Tomcat Start on Reboot: init.d script unsuccessful

I need to have tomcat start after a reboot of the linux OS. I cannot get init.d to function properly through reboot.
OS and Versions:
JRE: 1.8.0
JAVA: 1.8.0
Tomcat: 8.5.34
Linux: Amazon Linux 2
****ALL STEPS COMPLETED AS ROOT
TOMCAT Deployment Configuration:
1) Install tomcat 8.5.34 using a tar.gz gzip file
2) configure /{$TOMCAT}/conf/server.xml to use 443 connectors
3) Deploy MicroStrategy application through deploying a .war file on restart
4) configure SSL keys using Java Key Store
5) configure microstrategy webapp for SAML authentication using PING
init.d Script Deployment Configuration
Note: I have tried various scripts through /etc/init.d/tomcat and the chkconfig utility.
1) Create tomcat using vi
2) Insert script (I have tried numerous scripts, but this one seems to
be the clostest to exactly what I need and the most explicit)
3) chmod 755 /etc/init.d/tomcat
4) chkconfig --add tomcat
5) chkconfig --level 2345 tomcat on (This command is not successful)
6) chkconfig --list tomcat (returns tomcat 0:off 1:off 2:off 3:on 4:on 5:on 6:off)
Testing of this script is successful:
./etc/init.d/tomcat start
./etc/init.d/tomcat stop
./etc/init.d/tomcat restart
Confirmed that chkconfig created the links:
/etc/rc1.d K20tomcat
/etc/rc2.d K20tomcat
/etc/rc3.d S82tomcat
/etc/rc4.d S82tomcat
/etc/rc5.d S82tomcat
/etc/rc6.d K20tomcat
Script File for Tomcat
#!/bin/sh
#
# chkconfig: 345 82 20
#
# description: Tomcat Service
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
JRE_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
CATALINA_HOME=/opt/apache-tomcat-8.5.34
export JAVA_HOME JRE_HOME CATALINA_HOME
case $1 in
start)
cd $CATALINA_HOME/bin
./startup.sh
;;
stop)
cd $CATALINA_HOME/bin
./shutdown.sh
;;
restart)
cd $CATALINA_HOME/bin
./shutdown.sh
./startup.sh
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
Expectations
I expect the base URL at {$TOMCAT}/webapps/ROOT to be accessible from the Public URL pointing to this device following a reboot. The tomcat services remain in a stopped state after reboot.
Any suggestions?
Amazon Linux 2 uses systemd service manager which should be backwards compatible with systemv init scripts provided that systemd-sysv-generator is executed to generate service units out of /etc/init.d scripts (not recommended in your case I think).
Since you are writing the script yourself it is recommended that you write a proper service unit.
It's probable that such *.service file is already present on the tar.gz used to install tomcat.
Enable tomcat using systemd rather than systemv
Description
The script used is relatively simple because its only function is to start the server at reboot. I have established all of the required environment variables using setenv.sh in the TOMCAT bin.
Variables
TEST: Any alphanumeric value
TOMCAT_INSTALL_PATH: the location where you installed TOMCAT
Steps
Create a file tomcat#.service in /etc/systemd/system
A template can be found in /etc/systemd/system/multi-user.target.wants/tomcat.service
tomcat#.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment="CATALINA_HOME={TOMCAT_INSTALL_PATH}"
ExecStart=/opt/apache-tomcat-8.5.34/bin/startup.sh
ExecStop=$CATALINA_HOME/bin/shutdown.sh
SuccessExitStatus=143
User=root
[Install]
WantedBy=multi-user.target
create empty tomcat.pid file (mine was in {TOMCAT_INSTALL_PATH}/conf
write CATALINA_PID="{TOMCAT_INSTALL_PATH}/conf/tomcat.pid" line to setenv.sh in the {TOMCAT_INSTALL_PATH}/bin
systemctl daemon-reload
systemctl enable tomcat#test.service
systemctl start tomcat#test.service
Troubleshoot
systemctl status tomcat#test.service -l
-This command will provide log output to the console which displays the output which is also written to the systemctl log. I received errors on initial setup because tomcat could not interpret which was the main tomcat process and would close when reading the end of other processes. This was overcome by creating a pid file in the /conf folder and setting the CATALINA_PID variable in setenv.sh.

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!

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

Create a startup script for meteor in linux server

I have followed some posts and tutorials as well to create a script to start meteor project when server restart. i have followed answer mentioned in : How to run meteor on startup on Ubuntu server
Then I gave executable permission to script with "chmod +x meteor-server.sh".
I have tried to put this script in /etc/init.d and /etc/init folders but meteor project does not start at the reboot. I'm using ubuntu 16.04.
I would be grateful if you can show me the fault that i have done. Following code is my "meteor.server.sh" script file.
# meteorjs - meteorjs job file
description "MeteorJS"
author "Jc"
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
cd /home/me/projects/cricket
echo ""
end script
# Start the process
exec meteor run -p 4000 --help -- production
First of all, there's already a very good tool mupx that allows you to deploy meteor projects to your own architecture, so there's really no need to do it by yourself unless you have a very good.
If you really need to deploy manually, this will take several steps. I am not going to cover all the details because you're specifically asking about the startup script and the remaining instruction should be easily accessible in the Internet.
1. Prepare your server
Install MongoDB unless you are planning to use a remote database.
Install NodeJS.
Install reverse proxy server, e.g. Nginx, or haproxy.
Install Meteor, which we will use as the build tool only.
2. Build your app
I am assuming here that you already have the source code of your app put on the server to which you're planning to deploy. Go to your project root and run the following command:
meteor build /path/to/your/build --directory
Please note that if /path/to/your/build exists it will be recursively deleted first, so be careful with that.
3. Install app dependencies
Go to /path/to/your/build/bundle/programs/server and run:
npm install
4. Prepare a run.sh script
The file can be of the following form:
export MONGO_URL="mongodb://127.0.0.1:27017/appName"
export ROOT_URL="http://myapp.example.com"
export PORT=3000
export METEOR_SETTINGS="{}"
/usr/bin/env node /path/to/your/build/bundle/main.js
I will assume you put it in /path/to/your/run.sh. A couple of notes here:
This form of MONGO_URL assumes you have MongoDB installed locally.
You will need to instruct your reverse proxy server to point your app trafic to port 3000.
METEOR_SETTINGS should be the output of JSON.stringify(settings) of whatever settings object you may have.
5. Prepare upstart script
With all the preparations we've made so far, the script can be as simple as
description "node.js server"
start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]
respawn
script
exec /path/to/your/run.sh
end script
This file should go to /etc/init/appName.conf.
Finally i got it to work. I have used following 2 scripts to run meteor on the startup.
First i put this service file(meteor.service) in /etc/systemd/system
[Unit]
Description = My Meteor Application
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
I have called a scipt using this service. I put this following script(meteor.sh) in /etc/init.d
#!/bin/sh -
description "Meteor Projects"
author "Janitha"
#start service on following run levels
start on runlevel [2345]
#stop service on following run levels
stop on runlevel [016]
#restart service if crashed
respawn
#set user/group to run as
setuid janitha
setgid janitha
chdir /home/janitha/projects/cricket_app
#export HOME (for meteor), change dir to plex requests dir, and run meteor
script
export HOME=/home/janitha
exec meteor
end script
I make both these file executable by using
chmod +x meteor.service
chmod +x meteor.sh
And i have used following two commands to enable the service
systemctl daemon-reload
systemctl enable meteor.service
I used this configurations successfully
In /etc/init.d add a file called meteor.sh
#!/bin/sh
export HOME="/home/user"
cd /home/user/meteor/sparql-fedquest
meteor --allow-superuser
You must give executions permissions to meteor.sh
sudo chmod 644 meteor.sh
Also you must create meteor.service in /etc/systemd/system
[Unit]
Description =Portal of bibliographic resources of University of Cuenca
Author = Freddy Sumba
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
Also you must give permissions to meteor.service
$ sudo chmod 644 meteor.service
Then, we need add the service to this start each time that the server reboot
$ systemctl enable meteor.service
And finally start the service
$ service meteor start

Resources