Preface
之前使用solr进行全文检索,涉及到检索更新问题,这里采用定时更新方法,现在使用的系统为ubuntu,因此考虑crontab.
Solution Ideas
I. Preparation TOOLS
- Package Java program jar
- Installing crontab
Two. Writing crontab scripts
Process
I. Tool preparation
1. Make the jar package, either through the Java Jar command or through the Eclipse tool.
2. Installing crontab
Easier to install on Ubuntu:
sudo apt-get install crontab
Two. Writing crontab scripts
1. Understanding Crontab
Cron is a system daemon used to execute desired tasks (in the background) at designated times.
Cron belongs to a daemon that performs a specified task at a specific time, typically running in the background. The definition of the expression is very clear, when used to determine the time and command.
The format is simple:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
Simple example:
* * * * * command
Indicates that the command is executed every minute.
Problems encountered
(1) Its own Java program is just a test, is a simple pop-up dialog box, when writing crontab, there is no response to the situation.
View Log->/var/log/syslog
You can find the problem that appears in the log file: No MTA installed, discarding output
.
According to find the data found Crontab execution script is not directly wrong information output, but will be sent in the form of mail to your mailbox, this time you need a mail server, if you do not install the mail server, it will report this error.
By adding it after the crontab script > /dev/null 2>&1
, you can resolve the problem by redirecting the standard output to/dev/null and then redirecting the standard error to standard output, so that standard errors are redirected to the/dev/null because the standard output is redirected to/ Dev/null.
(2) Turn on crontab log
- Modify the Rsyslog file to
/etc/rsyslog.d/50-default.conf
delete the # before the #cron.* in the file;
- Restart the Rsyslog service
service rsyslog restart
;
- Restart the cron service
service cron restart
;
After restarting the service, there is still no dialog box, it is strange to see the log no errors appear.
After that, the display of the system needs to be started when the GUI application is started.
It is the possible to run GUI applications via Cronjobs. This can is done by the telling Cron which display to use.
00 06 * * * env DISPLAY=:0 gui_appname
The env display=:0 portion would tell Cron to use the current display
(desktop) for the program
可以在crontab里面设置env DISPLAY,也可以在shell程序里面添加:export DISPLAY=:0.0
DISPLAY=:0
The env display=:0 portion would tell Cron to use current display
the (desktop) for the program "Gui_appname".
DISPLAY=:0.0
If you had multiple monitors, don ' t forget to specify on which one of the program was to be run. The env display=:0.0 portion would tell cron-to the first screen of the current display
-use for the program "Gui_appname".
Summarize
- View official information (
优选
) and blog posts
- Encounter problems View log file, general, log will be recorded in very detailed
- Start with a simple example, step through the test, in order to verify whether the crontab is running, I started with a simple write log function test, and then the Java program test
- Go back to the first step
Appendix Writing Code
crontab代码
:
Execute test.sh script every minute
PATH=/usr/sbin:/usr/bin:/sbin:/bin/bin/bash /home/show_crontab/test.sh
test.sh
Script:
#!/bin/bashcd#进入目录export DISPLAY=:0.0#启动GUI显示java -jar test.jar # 以防万一,这里的文件最好写成绝对路经
Resources:
[1] Https://help.ubuntu.com/community/CronHowto
[2]http://www.cnblogs.com/daxian2012/articles/2589894.html
[3]http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html
[4]http://blog.csdn.net/huangyic1986/article/details/6731793
[5]http://bbs.chinaunix.net/thread-3650557-1-1.html
Use Crontab to execute Java programs under Ubuntu