0

How to ran Java Jar file in AWS EC2 server.

I have tried -

nohup java -jar myfilename.jar > output.log &

With the above approach, I will store the logs in output.log file. I want to rotate it after 150 MB completed, Without stopping the application.

Also tried using PM2 -

Create process.json

{
    "apps":[
    {
        "name": "my-backend-api",
        "cwd":".",
        "script":"/usr/bin/java",
        "args":[
            "-jar",
            "/home/ubuntu/my-backend/target/my-api-0.0.1-SNAPSHOT.jar"
        ],
        "watch":[
            "/home/ubuntu/my-backend/target/my-api-0.0.1-SNAPSHOT.jar"
        ],
        "node_args":[],
        "log_date_format":"YYYY-MM-DD HH:mm Z",
        "exec_interpreter":"",
        "exec_mode":"fork"
     }
   ]
}

pm2 start process.json

Does not seems to be working, each and every time, its restart the application. I don't want to do that.

Any help here ? Would be appreciated. Thank you in advanced

Atique Ahmed
  • 308
  • 4
  • 16

1 Answers1

0

We can ran our application -

nohup java -jar ./mybackend-api/target/myapp-api-0.0.1-SNAPSHOT.jar > ./logs/mylogs.log &

Install Logrotate - (For Ubuntu >= 18.0 version)

sudo apt update
sudo apt install logrotate

logrotate --version

Rotate Logs - https://crontab.guru/

5 * * * * // Every 5 minutes

/home/ubuntu/logs/*.log { missingok maxsize 5M rotate 2 compress copytruncate }

Run Cron Job -

crontab -e

Change in the file for every 5 minutes to change the logs -> 5 * * * * logrotate -f logrotate.conf

Restart CRON -

sudo service cron reload

Or

/etc/init.d/cron reload

This is for rotating logs, if you wanted to do so.

Atique Ahmed
  • 308
  • 4
  • 16