简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM