0

This is what my crontab looks like:

* * * * * /bin/sh /home/rathindu/assignment/test.sh

The test.sh file:

#!/bin/sh
mkdir new

The script is not running. But if I just open the terminal and invoke the script without using crontab it works perfectly.

When I inspect the CRON syslog

CRON[6909]: (CRON) info (No MTA installed, discarding output

This is what I get.

rathindu_w
  • 113
  • 3
  • That's because your cron job had some output and cron wanted to send your account an email with its content. I have a feeling it's trying to tell you that the mkdir command failed. – ajgringo619 Dec 08 '20 at 04:57
  • What is that you recommend me to do about this. I am really new to linux. – rathindu_w Dec 08 '20 at 04:58
  • use absolute path to /bin/mkdir /path/to/destination/new – αғsнιη Dec 08 '20 at 05:24
  • Thank you. It really helped. – rathindu_w Dec 08 '20 at 05:50
  • @αғsнιη is there anyway that instead of using absolute paths each time, just have kind of a path variable to current folder and then the rest can just be relative paths as it is. In simple terms I am looking for a way to tell cron that this is the current path to the folder, and all the other paths are relative to that particular path. Is that possible? – rathindu_w Dec 08 '20 at 05:57
  • @smooth-felix yes, switch to that directory before you want do things cd /path/to/where/your/stuff/is/there, but it's also better to use absolute path for any commands you are using because crontab has its own environment and maybe some commands it will still not able to find if you don't provide the full path – αғsнιη Dec 08 '20 at 06:05
  • 1
    @αғsнιη Thank you for your generous tips and advice. really appreciate – rathindu_w Dec 08 '20 at 06:58

1 Answers1

1

Just as @αғsнιη suggested in the comment, I replaced every relative path with an absolute path and it did worked perfectly. there was no need to use /bin/mkdir/ it just worked fine with simple mkdir. But the paths to the files had to be changed to their absolute paths

mkdir new

had to be changed to

mkdir /home/username/folder/new

And regarding the CRON[6909]: (CRON) info (No MTA installed, discarding output It was just a matter of installing a local mailbox:

apt-get install postfix

and then the mails can be found at:

tail -f /var/mail/<cron user>
rathindu_w
  • 113
  • 3
  • You will find the directory new in your home directory, where the first instance of your cron job created it. You can capture cron output in a log file rather than having it emailed to you, usually by appending > logfile 2>&1 to the command. – Chris Davies Dec 08 '20 at 07:08
  • Your previous version was actually creating the new directory into your home directory which is the cron's initial $PWD. – thanasisp Dec 08 '20 at 07:28