Method for shell script to run Java program jar
When deploying projects on Ubuntu, we often start programs through a shell, or even call Java programs regularly through crontab scheduled tasks. However, a strange problem is that, for example, I wrote the following shell script:
When the script is run manually from the command line, the Java program can run normally, but using crontab to schedule tasks seems to have no effect
Analyze possible causes:
1) Whether the current user does not have executable permissions for this shell script, through LS - LRT / apps / service / MTK / checking / run SH view script is executable, but it has execution permission -rwxr-xr-x
2) Since it's OK to run the script alone, is it a timing problem? So I wrote a simple shell script to output, and it's OK to pass the timing. The description is still a script problem.
Later, I checked the Internet and thought it might be the reason for the environment variables in the script. Because the script is run through crontab as the root user rather than the current user, so cat / etc / profile checks the environment variables and modifies the script as follows:
Analyze possible causes:
1) Whether the current user does not have executable permissions for this shell script, through LS - LRT / apps / service / MTK / checking / run SH view script is executable, but it has execution permission -rwxr-xr-x
2) Since it's OK to run the script alone, is it a timing problem? So I wrote a simple shell script to output, and it's OK to pass the timing. The description is still a script problem.
Later, I checked the Internet and thought it might be the reason for the environment variables in the script. Because the script is run through crontab as the root user rather than the current user, so cat / etc / profile checks the environment variables and modifies the script as follows:
Export displays the environment variables exported as user environment variables
翻译错误 Invalid Access Limit
Modified reference:
When the script is run manually from the command line, the Java program can be run normally, but using crontab to schedule tasks seems to have no effect. It is very depressed. Check the reasons and analyze the possible reasons:
1) Whether the current user does not have executable permissions for this shell script, through LS - LRT / apps / service / MTK / checking / run SH view script is executable, but it has execution permission -rwxr-xr-x
2) Since it's OK to run the script alone, is it a timing problem? So I wrote a simple shell script to output, and it's OK to pass the timing. The description is still a script problem.
Later, I checked the Internet and thought it might be the reason for the environment variables in the script. Because the script is run through crontab as the root user rather than the current user, so cat / etc / profile checks the environment variables and modifies the script as follows:
Export displays the environment variables exported as user environment variables
The above jar packages are exported through eclipse tool export and do not contain manifest MF file. If we use the packaging tool ant, we can package the default build Set class path in XML file
Add the third-party jar package to manifest.xml MF file, and specify the main class of the program
In build Add the following content to the XML:
In addition, when creating the manifest file, add:
Run ant in this way, and the generated jar package contains manifest The contents in MF are as follows:
In this way, there is no need to specify the jar package required by the program in the shell script, and there is no annoying problem of setting environment variables. This is also the case for more formal operations.
In this way, you can directly run the jar package in the shell: Java - jar main program jar -Xmx1024m -Xms1024m -Xmn512m,
#!/ Append after bin / Bash
source /etc/profile source ~/. bash_ profile
Test..
Java - CP ${classpath} main method package name The class name of the main method
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.