[英]Redirect the output to a file with date format name using crontab & nohup
Now I have a script start.sh
to start my application: 现在,我有一个脚本
start.sh
来启动我的应用程序:
java -Dlog4j.configuration=file:~/log4j.xml -Xmx2048m -jar ~/myapp.jar
And I add this script to crontab
to make it as a schedule job: 并将此脚本添加到
crontab
以使其作为计划作业:
15 05 * * 1-5 /home/me/start.sh >> /home/me/cronlog/$(date +"\%F")-start.log 2>&1
which redirects the output of start.sh
to a separate file with date format name 它将
start.sh
的输出重定向到日期格式名称为一个单独的文件
Right now, I'd like use nohup
and put the redirect command to the start.sh
script rather than in crontab
现在,我想使用
nohup
并将重定向命令放到start.sh
脚本中,而不是在crontab
So the crontab
looks clean: 所以
crontab
看起来很干净:
15 05 * * 1-5 /home/me/start.sh
And in the start.sh
, after testing it should be: 在
start.sh
,测试后应该是:
nohup java -Dlog4j.configuration=file:~/log4j.xml -Xmx2048m -jar ~/myapp.jar > ~/cronlog/$(date +"%F")-nohup.log 2>&1 &
Here is my question: 这是我的问题:
Why in crontab
, backslash \\
is needed but in the new start.sh
script, it isn't needed? 为什么在
crontab
中需要反斜杠\\
,但是在新的start.sh
脚本中却不需要反斜杠\\
?
I'd like to record every output to the file including exception or error, should it ends with up 2>&1 &
. 我想将每个输出记录到文件中,包括异常或错误,如果它以
2>&1 &
结尾。 The last &
is from nohup
. 最后一个
&
来自nohup
。 So what exactly do the 2>&1
and '&` mean? 那么
2>&1
和'&`到底是什么意思?
2>&1 means "redirect standard error on standard output" 2>&1表示“在标准输出上重定向标准错误”
http://www.tldp.org/LDP/abs/html/io-redirection.html http://www.tldp.org/LDP/abs/html/io-redirection.html
As you can see in that page, the numebers refer to the file descriptors used by the OS to point the resources (stderr, stdout), and &> is a redirection operator. 如您在该页面中所见,数字指的是操作系统用来指向资源(stderr,stdout)的文件描述符,而&>是重定向运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.