Understanding the output of Cron

bashcommand linecronscripts

I'm having trouble understanding the output of cron. Suppose I have a cronjob that runs every five minutes:

*/5 * * * * root /home/user/Desktop/shell.sh

shell.sh has the following contents:

#!/bin/bash
echo "hello"
bash -i

I have the following questions:

  1. Where is the job run? E.g. is a new TTY process started where the shell script is run?

  2. Where is the output sent? I've read that the output is sent to the mail of the owner. Does this mean root gets a mail with the echo statement?

  3. What about the bash command? Does the script start a bash process in its controlling terminal session which remains running until the terminal session ends (at the end of the script?)?

  4. Suppose I have reversed the shell code in the above bash script and I run a nc listener on my computer. Will this mean that every 5 minutes I will receive a connection that closes as soon as the script finishes running?

Apologies for the numerous questions. I'm new to this stuff. Thanks.

Best Answer

crontab is not exactly working like a classic shell.

  1. The job is run as root, but without any TTY. The command is run into a non-interactive environment, so bash -i probably returns an error or just closes once executed.

  2. Output is usually sent by mail. Ubuntu is usually shipped with a very minimal mail system called exim4. Look if file /var/mail/<your username> exists. You can try to read mail with:

    mail -u <your username>
    
  3. Because your script starts with #!/bin/bash, bash will be used, but this is not run in any terminal. Unless your script contains a loop, the process ends at the end of the script.

  4. I'm not sure I understand the use case with nc, but if you run nc from crontab to connect and send a message to your computer, nc and bash will end as soon the command is done.

Related Question