1

I am using nohup command to run a java web server application. This is the command i am using:

nohup java -jar WEB-SNAPSHOT.jar &

This command will create a nohup.out and my server logs are stored in this file. I want this file creation based on date i.e if current date is 2017-10-28, file should be created nohup.2017-10-28.out and when the date becomes 2017-10-29 12:00 AM nohup.2017-10-29.out file should be automatically created and so on . Example:

DATE       | File 
           |
2017-10-28 | nohup.2017-10-28.out
2017-10-29 | nohup.2017-10-29.out
2017-10-30 | nohup.2017-10-30.out 

2 Answers 2

3

You can just redirect the output to a file.

nohup java -jar WEB-SNAPSHOT.jar > nohup.$(date --iso).out

If you redirect, nohup will not create the default file, but will use the file specified with the redirect.
I also think you do not need the & at the end of your command.

4
  • Consider current date is 2017-10-28 at 2017-10-29 12:00 AM does this command create nohup.2017-10-29.out file
    – Ajit Soman
    Oct 28, 2017 at 15:24
  • Nope, nohup redirects to the file specified. It does not alter the output file. If you want to have a new filename you would have to stop the current instance and reexeute the command or use logrotate or similar.
    – Thomas
    Oct 28, 2017 at 15:28
  • Could you please answer with logrotate bcoz i am unable to find any useful result from google search
    – Ajit Soman
    Oct 28, 2017 at 16:11
  • & is needed if you want the application to continue running even after the current terminal session closes. Apr 6, 2022 at 10:53
1

If the goal is to background a process and log its stdout to a file, there are more features available if you implement it as a service.

Create a systemd service that starts a command similar to ExecStart=/usr/bin/java -jar /opt/app/WEB-SNAPSHOT.tar

Also set it to log to a unique syslog destination as described in systemd.exec

StandardOutput=syslog
SyslogIdentifier=app
SyslogFacility=local6

Set syslog to log that facility to its own file like /var/log/app.log

Configure /etc/logrotate.d/app to rotate the file. Use dateformat and postrotate scripts as necessary.

/var/log/app.log
2
  • Thanks for your answer but I am facing difficulty in understanding your answer as I am not a expert in linux. Could you provide me more information
    – Ajit Soman
    Oct 29, 2017 at 15:12
  • systemd manages many things including services. If you were to write a .service unit, you can start, stop, and manage the logging of a process. To learn more, read documentation like the RHEL guide to Creating and Modifying systemd Unit Files. Oct 29, 2017 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .