My goal is to run a script in crontab and save its output (via echo commands) to a log file. Thus, I am using the following:

$HOME/folder/sh/script.sh 1>$HOME/folder/logs/script.log 2>$HOME/folder/logs/script.err

However, from the command line (not cron yet), I cannot see the log file at all. The permissions are valid.

If I remove the redirections or keep the redirection to a file in the current directory, I can see the (correct) log contents.

Why doesn't bash script output appear in the log file under a nested folder structure?

  • 2
    Are all the components of the pathnames correctly spelled? Does the directory exist? Do you have write permission in the directory? Are you looking in the correct location when you look for the log files? – Kusalananda Aug 23 '18 at 9:13
  • Yes, yes, yes, yes. :D – cogitoergosum Aug 23 '18 at 9:15
  • 2
    If the shell failed to create the files, it ought to give you an error. You haven't mentioned any diagnostic output from the shell or script, so we must assume that the output was actually successfully written somewhere. A directory listing of the $HOME/folder/logs directory would be nice to see. – Kusalananda Aug 23 '18 at 9:22
  • 1
    Even an empty command, redirected to a file (e.g. : >file, a no-op command, redirected to file), will create the file (the shell creates the file before executing the command). You say that the files do not appear. This makes me believe that the redirection fails even before the script is executed. Either that, or your script is explicitly removing those files during the course of its execution. – Kusalananda Aug 23 '18 at 9:28
  • 1
    For the script to start at all, the files redirected into must be created (see previous comment). Are you sure nothing deletes the log files during the course of running the script? – Kusalananda Aug 23 '18 at 10:43

If you are sure about PATH, and permissions, and the same redirection works for other commands, it looks like your script is manipulating the log files or folders that you are trying to write (Ambiguous redirect).

It is that the case?

If so remove any lines of the script manipulating anything related to the logs, and try again.

If that works please select this as the correct answer.

If not please remove 2> run the script again, and watch what happen, maybe you will see a message showing why the script can't write to the files.

Good Lucky!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.