I have this sh script:

ufw allow 27017 && iptables -F

in a file test.sh

And I want to run this with a cronjob with root each day at 07:00 like this:

0 7 * * * /root/cron/test.sh

And I have also checked if this script actually runned with grep CRON /var/log/syslog and I can see that indeed it runned:

Aug 24 07:00:01 vps118774 CRON[1672]: (root) CMD (/root/cron/test.sh)

Now my problem is that that actual script from test.sh didn't run properly to unblock my port runned with that cronjob, the point is that if I run that script manually from terminal on the server with:

sh script.sh

All works fine, and the script will take the desired action,so the script runs as expected but what is wrong with my cron executing that?

I put the logs of the execution got these:

root/cron/test.sh: 2: /root/cron/test.sh: ufw: not found

  • 2
    You're testing your script with sh script.sh, but in the cronjob you run it without sh. If your script was not made executable, it will fail to run. Also you should add a shebang.
    – pLumo
    Commented Aug 24, 2018 at 8:50
  • Have you checked crontab owner (root I suppose) email for the output of the cron run?
    – andcoz
    Commented Aug 24, 2018 at 8:52
  • @RoVo how I can make it executable from cronjob? Commented Aug 24, 2018 at 9:00
  • @andcoz I posted the output at the end Commented Aug 24, 2018 at 9:00
  • What is output of which ufw. Which user you add the crontab? What is $PATH of that user?
    – pLumo
    Commented Aug 24, 2018 at 9:07

1 Answer 1


Cron jobs are run in a shell environment that may well be different from what your ordinary interactive shell environment is. For example, the PATH variable may have a list of different directories in it and may lack some directories that you are used to being able to execute utilities from.

Figure out where the ufw utility is located (using command -v ufw on the command line), then either

  1. use the complete path to that utility in the script, or
  2. modify the PATH variable inside the script so that it includes the directory in which the ufw utility (and iptables) may be found.

The script, as shown in the question, lacks a proper #!-line. This is not an issue if you run it with sh explicitly, but you don't do that in the cron job specification. Instead, make the script executable and then write it as



ufw allow 27017 && iptables -F

Then call the script from your crontab exactly like you're currently doing.

Here, I've also added the two directories /usr/sbin and /sbin to the PATH variable, just to show how one may do that in the script.

  • That was it, important information for me. Thank you! Commented Aug 24, 2018 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .