Linux run jar in background

Linux run jar in background

nohup java -jar xxx.jar >logs.log &
Let's make a detailed introduction. 

1. First, the most basic command to run the jar package is:

This command will lock the command window and you can only see the output information of the current operation. No other commands can be sent.

2. Let the jar package run in the background

Use the «&» symbol at the end to indicate that the program runs in the background.
In this case, the command window will not be locked, and other commands can be sent, but when the window is closed, the programs running in the background will still be stopped.

nohup command: The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hang-up signals. To run the nohup command in the background, add & (the symbol for «and») to the end of the command.

Simply put, the nohup command can prevent the hangup signal that the window is closed, so that the program continues to run. In this way, the command is modified to

The required function can be achieved under this command. The last >logs.txt represents the output file. You can write and specify the path at will. If it is not written (default), it will return to the default directory where the jar package is located and create the nohup.out file.

If the log output has been specified in the project, the output will be repeated and two files will be generated. If >logslog is deleted, the current running program will not generate new files.

nohup java -jar xxx.jar >logs.log &
  1. After the command runs successfully, it will return a process number, which can be directly shut down by killing the process with the kill -9 command.

If you forget the process number, you can pass

To view the process number of the currently running jar package program.

Источник

Linux run jar in background

Recently I played on my own server. After my project was packaged into a jar, I used java-jar xxx.jar on Linux and it could run normally, but my shell was locked and I could not perform other operations unless I opened another one. A terminal or use Ctrl+C to end the current program, obviously this does not meet my needs:

java -jar server-0.0.1-SNAPSHOT.jar

2. Run in the background

Add & after the command, then the process can run in the background:

java -jar server-0.0.1-SNAPSHOT.jar &

But there is still a problem. Once I close the shell window, the process will terminate:

Читайте также:  Linux nano copy paste

I need this project to run all the time, so obviously this method can’t meet my needs. To

3. Nohup mode operation

nohup means not to hang up the running command, even if the shell is closed, the program will still run in the background.

nohup java -jar server-0.0.1-SNAPSHOT.jar >log.log &

The >log.log indicates that the output of the program is redirected to the log.log file. By default, it is output to the nohup.out file in the current directory. To

When I exit the current shell, open a new window, and look again, I can find that the process is still executing:

So how can we terminate this process? It’s very simple, just use the kill command:

Источник

Run java jar file on a server as background process

I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can’t shut down / terminate the process. If I press ctrl + C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?

 #!/bin/sh java -jar /web/server.jar echo $! #> startupApp.pid 

4 Answers 4

#!/bin/sh nohup java -jar /web/server.jar & 

The & symbol, switches the program to run in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.

Thanks Anton,currntly im stopping the server by killing the process id.i don’t think it’s best practice.is thery command to to stop the server?

Short answer: it depends on the server. Long answer: as far as I know, there is no safe way to shut down a process without the process supporting such graceful shut downs. For example, if it’s a web server being terminated by an external signal, there is always a possibility that some requests will be lost. One way to solve this problem is to implement a graceful termination function in the server itself, e.g. by processing a special kind of requests. Then, the server can be terminated by sending it a request of that special kind. Otherwise, killing the process by its ID is the simple way.

Using nohup should always be combined with redirecting stdout and stderr explicitly — otherwise, you don’t get control of where the logs go, and end up with an ugly nohup.out created in whichever directory this script happens to be invoked from.

@Anton when I run a script(just like your answer) with SSH EXEC runjar.sh. It started streaming the logs in my local terminal and if close my terminal my jar gets killed. How can I run a jar in the background using SSH EXEC?

Читайте также:  Windows не видит загрузчик linux

Источник

Run Jar in Background on Linux

I have a jar that runs forever (infinite loop with socket listening thread) and need it to run in the background at all times. An example would be: «java -jar test.jar» How do I do this? Thanks in advance!

7 Answers 7

  1. appending & at the back. However, using this, the program will still be terminated if you closed the terminal that started the program.
  2. Start a screen session, and start the program inside it; you can detach the screen session and close the terminal. Later on, you can attach to the session again and found yourself back on the console as if you’ve been there all along. However, you will need to start a screen session before running the program, and if you forgot to do that, you can’t do anything about it.
  3. Use disown job control from your shell. This will detach the task from your tty and your program won’t be terminated when the tty is closed. However, I don’t think there’s any way to reattach a disowned job.

A command line prompt I have always used for long run times to last through logoffs is «nohup» so in your case is

The & is important so you can get another shell running. I believe this will not last through reboots.

If by «at all times», you mean it gets started when the machine boots, you’ll need to start it as part of the boot-time scripts. The dirty way to do this is to add

java -jar test.jar >/dev/null 2>&1 &

to /etc/rc.local (or replace /dev/null with your favorite log file).

Better would be to make an init.d script that gets started at the appropriate runlevels (you might not want it on runlevel 0, 1 or 6, for instance). That way, you can also have it be restarted automatically if it dies and have a nicer interface to temporarily stopping it.

Источник

Run Jar in Background on Linux

The & is important so you can get another shell running. I believe this will not last through reboots.

Solution 3

If by «at all times», you mean it gets started when the machine boots, you’ll need to start it as part of the boot-time scripts. The dirty way to do this is to add

java -jar test.jar >/dev/null 2>&1 &

to /etc/rc.local (or replace /dev/null with your favorite log file).

Better would be to make an init.d script that gets started at the appropriate runlevels (you might not want it on runlevel 0, 1 or 6, for instance). That way, you can also have it be restarted automatically if it dies and have a nicer interface to temporarily stopping it.

Читайте также:  Install linux on my samsung

Solution 4

The & operator will force the process to run in the background, you can run the ‘top’ command afterwards to see that it is running.

Solution 5

You can run any task in the background on Linux by appending a ‘&’ symbol to the end of the command-line, e.g.

Govind Parmar

Govind Parmar

Updated on September 17, 2022

Comments

Govind Parmar

So, I’m testing the following function FindPixel with the following app. The HWND and COLORREF are constants that I’ve determined with Spy++ and Color Cop for debugging; the final version of the program will find these automatically. I’ve confirmed that the part of this algorithm which determines whether the color exists works (ie: if the color exists anywhere in the window, the if statement for that is true eventually, and if it doesn’t the if statement is never true), however, I cannot figure out how to isolate which pixel this occurs on. The line SetCursorPos(rect.left+i, rect.top+i2); does not move the mouse anywhere near the correct location. The window I’m debugging this with is entirely white save for the one pixel with the value 16776960. The function can tell that it’s there, but the values of (i, i2) are not the (x, y) coordinates of where it occurs. Is there something I’m missing here?

#include void FindPixel(HWND hWnd, COLORREF target) < HDC hDC = GetDC(hWnd); HDC memDC = CreateCompatibleDC (hDC); BYTE *ScreenData = NULL; HBITMAP hBitmap; BITMAPINFOHEADER bmHeader = ; RECT rect; int width, height; int i, i2; GetWindowRect(hWnd, &rect); width = rect.right-rect.left; height = rect.bottom-rect.top; ScreenData = (BYTE*)malloc(4*width*height); hBitmap = CreateCompatibleBitmap(hDC, width, height); bmHeader.biSize = sizeof(BITMAPINFOHEADER); bmHeader.biPlanes = 1; bmHeader.biBitCount = 24; bmHeader.biWidth = width; bmHeader.biHeight = -height; bmHeader.biCompression = BI_RGB; SelectObject(memDC, hBitmap); BitBlt(memDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY); GetDIBits(hDC, hBitmap, 0, height, ScreenData, (BITMAPINFO*)&bmHeader, DIB_RGB_COLORS); // i=0; for(i = 0; i < width; i++) < for(i2 = 0; i2 < height; i2++) < if(RGB((ScreenData[3*((i2*width)+i)+2]),(ScreenData[3*((i2*width)+i)+1]), ScreenData[3*((i2*width)+i)])==target) < SetCursorPos(rect.left+i, rect.top+i2); DeleteObject(hBitmap); DeleteDC(memDC); free(ScreenData); ReleaseDC(hWnd, hDC); return; >> > DeleteObject(hBitmap); DeleteDC(memDC); free(ScreenData); ReleaseDC(hWnd, hDC); > int APIENTRY WinMain(HINSTANCE hi, HINSTANCE hpi, LPSTR lpcl, int nsc)

Does the window have a standard border? You are working with non-client coordinates when you call GetWindowRect , but GetDC will return a DC for the client area, meaning your x/y coordinates will be off by the top/left non-client frame size when you call SetCursorPos .

You also have to take the stride into account. There can be padding at the end of each row of pixels. The stride is computed by ((width * bits_per_pixel) + 7) / 8

Govind Parmar

Yeah, turns once I took the stride into account the function finds it correctly 🙂 Thanks @JimMischel

another possibility, if you want it to run with just user perms is to put it in cron and use ‘@reboot’ as the time to run. See ‘man 5 crontab’ for details.

Источник

Оцените статью
Adblock
detector