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.
Related
Greatings to you first
I am a student at the university, and my end of study project is to master an information security protocol for autonomous systems
I have a task in this project
when I type in the command line "kill [pid]", the process starts automatically after a delay of a few seconds
how I can achieve this task and thank you in advance
use systemd service
Create a file test.service under /etc/systemd/system, such as:
[Unit]
Description=test service
[Service]
User=root
Restart=always
# number of seconds to wait before restarting
RestartSec=5
# Change it to some meaningful processes
ExecStart=/bin/sleep 30000
[Install]
WantedBy=multi-user.target
To start the service: sudo systemctl start test.service
Then if you kill the process, it will automatically restart in 5 seconds
Note: if you modify the service file, you will have to run sudo systemctl daemon-reload and sudo systemctl restart test to reload the change
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
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
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.
I have recently completed the Wiki web development tutorial (http://golang.org/doc/articles/wiki/). I had tons of fun and I would like to experiment more with the net/http package.
However, I noticed that when I run the wiki from a console, the wiki takes over the console. If I close the console terminal or stop the process with CTRL+Z then the server stops.
How can I get the server to run in the background? I think the term for that is running in a daemon.
I'm running this on Ubuntu 12.04. Thanks for any help.
Simple / Usable things first
If you want a start script without much effort (i.e. dealing with the process, just having it managed by the system), you could create a systemd service. See Greg's answer for a detailled description on how to do that.
Afterwards you can start the service with
systemctl start myserver
Previously I would have recommended trying xinetd or something similar for finer granuarlity regarding resource and permission management but systemd already covers that.
Using the shell
You could start your process like this:
nohup ./myexecutable &
The & tells the shell to start the command in the background, keeping it in the job list.
On some shells, the job is killed if the parent shell exits using the HANGUP signal.
To prevent this, you can launch your command using the nohup command, which discards the HANGUP signal.
However, this does not work, if the called process reconnects the HANGUP signal.
To be really sure, you need to remove the process from the shell's joblist.
For two well known shells this can be achieved as follows:
bash:
./myexecutable &
disown <pid>
zsh:
./myexecutable &!
Killing your background job
Normally, the shell prints the PID of the process, which then can be killed using the kill command, to stop the server. If your shell does not print the PID, you can get it using
echo $!
directly after execution. This prints the PID of the forked process.
You could use Supervisord to manage your process.
Ubuntu? Use upstart.
Create a file in /etc/init for your job, named your-service-name.conf
start on net-device-up
exec /path/to/file --option
You can use start your-service-name, as well as: stop, restart, status
This will configure your service using systemd, not a comprehensive tutorial but rather a quick jump-start of how this can be set up.
Content of your app.service file
[Unit]
Description=deploy-webhook service
After=network.target
[Service]
ExecStart=/usr/bin/go webhook.go
WorkingDirectory=/etc/deploy-webhook
User=app-svc
Group=app-svc
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=deploy-webhook-service
PrivateTmp=true
Environment=APP_PARAM_1=ParamA
Environment=APP_PARAM_2=ParamB
[Install]
WantedBy=multi-user.target
Starting the Service
sudo systemctl start deploy-webhook.service
Service Status
sudo systemctl status deploy-webhook.service
Logs
journalctl -u deploy-webhook -e
After you press ctrl+z (putting the current task to sleep) you can run the command bg in the terminal (stands for background) to let the latest task continue running in the background.
When you need to, run fg to get back to the task.
To get the same result, you can add to your command & at the end to start it in the background.
To add to Greg's answer:
To run the Go App as a service you need to create a new service unit file.
However, the App needs to know where Go is installed. The easiest way to lookup that location is by running this command:
which go
which gives you an output like this:
/usr/local/go/bin/go
With this piece of information, you can create the systemd service file. Create a file named providus-app.service in the /etc/systemd/system/ using the command below:
sudo touch /etc/systemd/system/providus-app.service
Next open the newly created file:
sudo nano /etc/systemd/system/providus-app.service
Paste the following configuration into your service file:
[Unit]
Description=Providus App Service
After=network.target
[Service]
Type=forking
User=deploy
Group=deploy
ExecStart=/usr/local/go/bin/go run main.go
WorkingDirectory=/home/deploy/providus-app
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=providus-app-service
PrivateTmp=true
[Install]
WantedBy=multi-user.target
When you are finished, save and close the file.
Next, reload the systemd daemon so that it knows about our service file:
sudo systemctl daemon-reload
Start the Providus App service by typing:
sudo systemctl restart providus-app
Double-check that it started without errors by typing:
sudo systemctl status providus-app
And then enable the Providus App service file so that Providus App automatically starts at boot, that is, it can start on its own whenever the server restarts:
sudo systemctl enable providus-app
This creates a multi-user.target symlink in /etc/systemd/system/multi-user.target.wants/providus-app.service for the /etc/systemd/system/providus-app.service file that you created.
To check logs:
sudo journalctl -u providus-app