1

I'm trying to run a cron job that executes a script as a user using crontab -e on ubuntu server 16.04 LTS. The script is and located in /data/autoremove.sh:

!/usr/bin/env bash

if ping -q -c 1 -W 1 1.1.1.1 >/dev/null; then scp /data/Data* user@1.1.1.1:/data && rm data* else echo "does not work" fi

and the cron job is being configured with the user "user":

*/1 * * * * /data/autoremove.sh>/dev/null 2> ~/cron.err.

I have tried adding both a SHELL=/bin/bash and a PATH= to both the crontab and the script, and confirmed that cron is running, and attempted to run it without declaring a user. The script works when running it either as root or the owner

The error I am getting is /bin/bash: user: command not found' I've gone through multiple articles on the different stacks sites and not had any luck getting this to work.

  • Why do you think that's how you specify the user to run it as? – Ignacio Vazquez-Abrams Aug 2 '18 at 17:20
  • @Ignacio Vazquez-Abrams because that is what the manual says help.ubuntu.com/community/CronHowto ...don't think I understand what you are getting at. – risail Aug 2 '18 at 18:43
  • When you using user's cron, you shouldn't specify user, it is applied only to system cron. Other things - use everywhere full paths – Alex Aug 2 '18 at 19:31
  • Error in first line: replace it to #!/bin/bash instead of !/usr/bin/env bash – Alex Aug 2 '18 at 19:40
  • ~/autoremove.sh is in your home directory, while /data/autoremove.sh is in the directory /data – Alex Aug 2 '18 at 19:47

Your Answer

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

Browse other questions tagged or ask your own question.