在java开发中,有时会遇到有些中间件,或者web项目因为一些问题需要重新启动(我这边主要是需要像定时任务一样) 有两种方法
一,java方法
public class test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String shpath="/srv/www/app/ZTFrame/resinctl restart"; //程序路径
Process process =null;
String command1 = shpath;
process = Runtime.getRuntime().exec(command1);
process.waitFor();
System.out.println("执行成功");
}
}
二,linux自带任务方式
crontab -l
增加任务
输入crontab -e 然后编辑任务如下,表示每个月1号晚上12点整执行一次同步
00 00 1 * * /usr/sbin/ntpdate
10.137.31.161
重启服务: service crond restart
(这种需要注意,当linux机器重启之后,需要验证系统中定时任务是否关闭)
增加任务
输入crontab -e 然后编辑任务如下,表示每个月1号晚上12点整执行一次同步
00 00 1 * * /usr/sbin/ntpdate
重启服务: service crond restart