Create nohup.out file based on date

5,507

Solution 1

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.

Solution 2

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
Share:
5,507

Related videos on Youtube

Ajit Soman
Author by

Ajit Soman

I am a full stack developer specialized in Java and Javascript. I write clean and readable code :) . I am very fond of delivering quality code and building scalable application.

Updated on September 18, 2022

Comments

  • Ajit Soman
    Ajit Soman over 1 year

    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 
    
  • Ajit Soman
    Ajit Soman over 6 years
    Consider current date is 2017-10-28 at 2017-10-29 12:00 AM does this command create nohup.2017-10-29.out file
  • Thomas
    Thomas over 6 years
    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.
  • Ajit Soman
    Ajit Soman over 6 years
    Could you please answer with logrotate bcoz i am unable to find any useful result from google search
  • Ajit Soman
    Ajit Soman over 6 years
    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
  • John Mahowald
    John Mahowald over 6 years
    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.
  • Amimo Benja
    Amimo Benja about 2 years
    & is needed if you want the application to continue running even after the current terminal session closes.