Use Crontab to execute Java programs under Ubuntu

Source: Internet
Author: User
Tags rsyslog

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=:0The env display=:0 portion would tell Cron to use current display the (desktop) for the program "Gui_appname".
    • DISPLAY=:0.0If 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
    1. View official information ( 优选 ) and blog posts
    2. Encounter problems View log file, general, log will be recorded in very detailed
    3. 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
    4. 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.shScript:

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.