How can I pass stdout and stderr in a Command Line (cmd) job to the Mitto logs?

I’d like to take the output and errors printed to the screen in a terminal command and print those to the Mitto logs in a cmd job. Are there any best practices on how to do this?

Every process in Linux uses three open files called file descriptors. These files are the standard input, standard output and standard error files. By default :

  1. Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
  2. Standard Output is the terminal from which the script runs, also abstracted as a file to make writing scripts & program easier.
  3. Standard error is similar to standard output but contains only the errors.

If you have a command job that you want to write the output to a file for, add >>/var/mitto/log/<logname> to the end of the command and what would have been written to standard output, normally the terminal, will instead be appended to a file on the filesystem called /var/mitto/log/. Failure to exactly reproduce this command could overwrite your existing logs. Please see note below.

If you need to only redirect the output from the stderr (standard error), use 2>>/var/mitto/log/<logname>
Failure to exactly reproduce this command could overwrite your existing logs. Please see note below.

If you want both to go to the file, use >>/var/mitto/log/<logname> 2>&1.
Failure to exactly reproduce this command could overwrite your existing logs. Please see note below.

**It is important to use the double greater than signs (>>); failure to do so will overwrite the existing log. It is suggested that the first time you do it, you make a backup of the existing log.

Vicki

Thanks for the reply Vicki. In my case I do already have the output going to a file, but I would find it more useful if it were displayed in the Mitto job log, so I can see progress through the UI as the job is running.

Also, in the case of a failed job, in one instance the Mitto job error shows:
The command returned with non-zero exit status 1 - no standard error available

It would be very helpful to be able to configure the cmd job so whatever error is returned is passed through to the UI. Is this possible?

Hey Ben:

Yes, you should be able to do that by using the Mitto job log name (with full path) where I have <logname> above. As long as you are using the append operator ‘>>’, it should simply append to that log. I would try it out with a dummy log first to be sure that you have it right though, because it is easy to overwrite a log if you forget to use the append operator or if you do any number of other small things wrong. You might also want to make a backup of the log, but that could cause you to lose any entries that are written while you are testing.

Please let me know if this is unclear or if you want to jump on a call to work it out.

Vicki

Thanks Vicki, and apologies for misunderstanding your original response.

This works great, with one exception - the log data does not seem to update real-time on the /logs page. Is there an argument that will force the logs to be updated the same way the native Mitto logs update on the page in real-time?