简体   繁体   中英

Running bash script as a service and write to another bash script is not working

I have the following problem using bash script.

Here is what I have inside the 'startup' script file:

#!/bin/bash

java -cp ../lib/online-store.jar:../lib/* com.online.store.Main

OnlineStorePID=$! 

if [$OnlineStorePID -ne 0] then
    echo "kill $OnlineStorePID" > shutdown
fi

Basically what I do, is to run a java application, get the process id and write it to another bash file. All this process works when I execute the startup script, and the 'shutdown' script file is updated successfully with a line containing 'kill processIDNumber' cmd.

Now I have tried to create a service on Ubuntu for this script using the following commands:

sudo systemctl daemon-reload
sudo systemctl enable online-store.service
sudo systemctl start online-store

When I start the service the java application starts successfully, but the shutdown script file is not updated. It seems that the 'echo "kill $OnlineStorePID" > shutdown' line is not executed. I don't get any complain errors. Does anyone knows what's the problem here.

Here is my service file:

[Unit]
Description=Online store service
Requires=multi-user.target
After=multi-user.target
Wants=mysql.service

[Service]
WorkingDirectory=/home/user/Desktop/online-store-service

#path to executable. 
ExecStart=/home/user/Desktop/online-store-service/bin/startup
ExecStop=/home/user/Desktop/online-store-service/bin/shutdown

SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

更改您的脚本并运行如下所示的java命令作为后台进程

java -cp ../lib/online-store.jar:../lib/* com.online.store.Main  >/dev/null 2>&1 &

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