【问题标题】:Restarting Services in Linux after a Server Reboot服务器重新启动后在 Linux 中重新启动服务
【发布时间】:2019-02-23 18:15:32
【问题描述】:

所以今天我们的一个应用服务器由于某些问题重新启动,重新启动后我们发现我们的应用服务没有运行。

我想创建一个脚本,它会在服务器重新启动后检查以下这些服务,并在发现它们停止时自动启动它们:

第一个服务路径:/opt/bea/config/nm/nm-sdi-abc/beaNMctl.sh

第二个服务 TOMCAT - 路径:/opt/apache/tomcat/bin --- 服务名称 startup.sh

这里是第一个服务可以使用我使用的普通 id 帐户启动。

但第二个服务可以在登录到同一服务器和网络上的不同服务帐户后重新启动。如下:

[x201691@abc bin]$ su - apache

密码:

-bash-2.05b$ cd /

-bash-2.05b$ cd /opt/apache/tomcat/bin/

-bash-2.05b$ ./startup.sh

有人可以帮忙吗?

我们也不是 root 用户。

【问题讨论】:

    标签: linux tomcat nodemanager


    【解决方案1】:

    你可以写一个shell脚本:

    echo YOUR_PASSWORD | sudo -S su 
    cd /opt/apache/tomcat/bin/ 
    ./startup.sh
    

    将此保存为您有权访问的文件并添加以下 cron 条目:

    @reboot MYPATH/myscript.sh >>  MYPATH/script.log 2>&1
    

    script.log 将包含脚本的任何输出或错误。您可以将date 命令添加到脚本中,以帮助了解有关它何时运行的信息。更多关于cron的信息请点击此处。

    另外,如果您对在脚本中输入密码有疑问,可以通过讨论here

    【讨论】:

    • 嗨 mbsingh ...我可以在那里创建一个 cron 作业,没有问题。我正在寻找的是一个脚本,它将在服务器重新启动时启动第一个服务和第二个服务本身?
    • 您是否在脚本中寻找特定内容?我认为仅更改用户 ID 是上面脚本已经存在的问题。 cron 条目开头的@reboot 意味着脚本将在服务器启动时运行。
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2019-08-23
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      相关资源
      最近更新 更多