Search code examples
linuxscriptingredhattcsh

jobs -l command fails to write to file using csh


We currently have a script where it checks if things are running in the background. It does this by running the following (updated to include a full test script example which I ran on our server):

#!/bin/csh -f
sleep 30 &
set testFile = /tmp/checkFile$$
jobs -l > $testFile

It prints to the screen the processes running but nothing appears in the check file, thus causing the server to think the other pieces failed to start). At a complete loss on why this doesn't work. Other commands appear to work fine.

I am running rhel 6.8 and running the command as a non-root user. . Running other commands such as ps > /tmp/testFile worked without issues.

After further digging on the system, the following notes are added:

  • /bin/csh is a softlink to /bin/tcsh (I removed the csh tag)
  • Changing the shebang line to /bin/tcsh -f did not change anything (obviously but wanted to state)
  • file is created but the contents are empty

Solution

  • In some versions of tcsh, the jobs -l command prints its output to stderr. In others it prints to stdout. (I'd say printing to stdout is the correct behavior.)

    The workaround is to change

    jobs -l > $testFile # redirect stdout
    

    to

    jobs -l >& $testFile # redirect both stdout and stderr
    

    This should work correctly for any version of tcsh, with or without the bug.

    Note that tcsh on the OP's system is a symbolic link to csh, so this is probably specifically a tcsh issue.

    There's a mirror of the tcsh sources at https://github.com/tcsh-org/tcsh . The log shows a change made 2016-05-24, "don't print jobs to stderr (paulo.cesar.pereira.de.andrade)". Looking at the commit history, it appears that jobs output is printed to stdout in tcsh versions 6.19.01 and 6.20, and to stderr in versions 6.19.00 and earlier. Apparently some packages on the OP's newer RHEL 6.8 system have been updated.

    I see jobs -l writing to stdout in tcsh 6.14.00 and 6.18.01. It might have been a regression. (I've been unable to build tcsh 6.19 or earlier from source.)