Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use crontab to run Java program timed tasks in Linux

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use crontab in Linux to run Java program scheduled tasks, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Analysis.

Its command format and main parameters are as follows:

Command format crontab [- u user] [- e |-l |-r] command parameter-u user: used to set a user's crontab service; file:file is the name of the command file, indicating that file is used as a task list file for crontab and loaded into crontab. If this file is not specified on the command line, the > crontab command accepts commands typed on the standard input (keyboard) and loads them into crontab. -e: edit the contents of a user's crontab file. If you do not specify a user, you edit the current user's crontab file. -l: displays the crontab file contents of a user. If no user is specified, the crontab file contents of the current user are displayed. -r: delete a user's crontab file from the / var/spool/cron directory. If no user is specified, the current user's crontab file is deleted by default. -I: give a confirmation prompt when deleting the user's crontab file.

Pictorial format description:

This time, because I need to push a batch of data, I write a jar program with Java to finish using the command line java-jar push.jar call, but the data is pushed every day, so do I have to execute the command line manually every day? This is obviously impractical.

It is too troublesome to develop a scheduled task for such a Mini Program. Later, I thought of the crontab of the Linux system, but I still encountered a few problems in the process of using it. Record it here.

Realize

The first step is to write a start_pust.sh file, which is simple as follows:

#! / bin/bashjava-jar / home/liyd/push.jar

In order to avoid errors caused by path problems, absolute paths are used here to ensure correct execution.

The second step is to write the crontab.txt file, with a simple line specifying the execution time:

34 1 * / home/liyd/start_push.sh

Push.jar is called at 01:34 every day.

Finally, specify crontab to run:

Crontab crontab.txt

This is all set up, and we expect that pust.jar push data will be executed at 01:34 every day.

Encounter a problem again

But things often don't work out as we expected, and we find that the program hasn't been executed at all. Why? Because before that, I could use direct. / start_push.sh from the command line.

I can only check the information. I found that many people have encountered this problem on the Internet. To sum up, there are two points:

I. path problem

II. Environmental variables

Here I have used the absolute path to avoid mistakes, which seems to be the problem of environment variables.

It turns out that crontab will not load the environment variable configuration, which needs to be set in the script. The Java program cannot be run without environment variables such as JDK.

Modify the previous start_push.sh script to add reading of the profile file:

#! / bin/bash. / etc/profile. ~ / .bash_profilejava-jar / home/liyd/push.jar

At this point, the program can run normally. Using ps aux | grep java, you can see the executing process, but my Java program has no log output. Check my log configuration:

It should have been output to the logs folder of the current project, but the preliminary estimate here should be that the variable ${user.dir} cannot be found.

Modify the start_pust.sh file again and add the user.dir parameter:

#! / bin/bash. / etc/profile. Bash_profilejava-Duser.dir= "/ home/liyd/"-jar / home/liyd/push.jar above is how to use crontab to run scheduled tasks in Java programs in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report