我编写了一个简单的java程序,每隔几秒打印一次,无限while
循环,Thread.sleep()
。
像java -jar ScheduledTask.jar
那样运行它会产生预期的打印到文件。
我按照建议HERE包装了jar,将其放在/etc/init.d/test1
中,test1
是我的包装文件,然后运行sudo chmod +x /etc/init.d/test1
。
test1
显示在service --status-all
列表
然而,当我运行sudo service test1 start
时,它不会进行打印。
我可以提供java代码,但它所做的只是打印到文件。
日志文件位于/usr/local/test1/test.log
系统是具有root权限的Debian 8(在AWS上)。一切都是通过终端(putty)完成的。
有什么不对?
打包机:
#!/bin/sh
SERVICE_NAME=test1
PATH_TO_JAR=/usr/local/test1/ScheduledTask.jar
PID_PATH_NAME=/tmp/test1-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
Java代码:
public class BaseServiceEngine {
public static void main(String args[]) {
System.out.println("program started");
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter writer = null;
String filePath = "/usr/local/test1/test.log";
String osname = System.getProperty("os.name", "").toLowerCase();
if (osname.startsWith("windows")) {
filePath = "C:\\Development\\VS2012\\TFS\\ProjectCX\\WorkSpace.Alex.Main\\ProxyServers\\ProxyConfigurationService\\test.log";
} else if (osname.startsWith("linux")) {
filePath = "/usr/local/test1/test.log";
} else {
System.out.println("Sorry, failed to locate file");
return;
}
File logFile = new File(filePath);
try {
logFile.createNewFile();
fw = new FileWriter(filePath, true);
bw = new BufferedWriter(fw);
writer = new PrintWriter(bw);
writer.println("Service started: " + new Date());
final int runEveryXMillisecond = 20000;
int i = 0;
boolean runService = true;
Date startTime = new Date();
System.out.println("starting to print to filePath");
while (runService) {
Thread.sleep(2500);
if (new Date().getTime() - startTime.getTime() >= runEveryXMillisecond) {
writer.println("Process run");
}
Date now = new Date();
writer.println("service Time :" + now + "__loop:" + i++);
writer.flush();
}
} catch (IOException | InterruptedException ex) {
System.out.println(ex.getCause());
} finally {
try {
writer.close();
bw.close();
fw.close();
} catch (IOException | NullPointerException ex) { }
}
System.exit(0);
System.out.println("Exiting");
}
}
sudo service test1 status
返回:
test1.service - (null)已加载:已加载(/etc/init.d/test1)
活跃:自2016年11月15日星期三09:55:23 UTC开始活跃(正在运行); 1分0秒 前进程序:26062 ExecStart = / etc / init.d / test1 start(code = exited, status = 0 / SUCCESS)CGroup:/system.slice/test1.service └─26063java-jar /usr/local/test1/ScheduledTask.jar / tmp5月11日09:55:23 ip-10-0-0-115 test1 [26062]:开始测试1 ... 5月11日 09:55:23 ip-10-0-0-115 test1 [26062]:test1开始... 5月11日09:55:23 ip-10-0-0-115 systemd 1:已启动(null)。