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:
-
Where is the job run? E.g. is a new TTY process started where the shell script is run?
-
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? -
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?)? -
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.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.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: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.I'm not sure I understand the use case with
nc
, but if you runnc
fromcrontab
to connect and send a message to your computer,nc
andbash
will end as soon the command is done.