Shell script and CRON problems

15,827

Solution 1

The clue is in the error message:

/bin/sh: cannot create : nonexistent

Notice that it says "sh". The Bourne shell doesn't support some features that are specific to Bash. If you're using Bash features, then you need to tell Bash to run the script.

Make the first line of your file:

#!/bin/bash

or in your crontab entry do this:

* * * * * /bin/bash scriptname

Without seeing your crontab entry and your script it's hard to be any more specific.

Solution 2

Perhaps the first thing you should do in your backups.sh is insert a cd /home/user1. crond may execute your script from a different directory than you think it does, and forcing it to use the same directory regardless of how it is executed could be a good first start.

Another potentially useful debugging step is to add id > /tmp/id.$$ or something like that, so you can see exactly which user account and groups are being used to run your script.

Solution 3

In crontab, just change 2>$1 to 2>&1. I've just done one myself. Thank you Dennis Williamson.

Share:
15,827
Pete
Author by

Pete

I'm a web developer/full stack developer/programmer/nerd/internet person what ever you want to call me, I currently make websites and APIs, with CFML(ACF and Lucee), Node.js and Vue.js. I've done some Native iOS and Phonegap/Cordova iOS and Android work in the past. I'll can fix a computer, manage servers, monitor logs, build databases, optimize queries, shrink download sizes, speed up sites, write back end code, build front end interfaces, analyze user behaviours, get conversions and leads... just don't ask me to design anything. I'm a father, husband, 1st degree taekwondo black belt and Grade 2 guitarist who likes drawing, driving, gaming and all the usual sci-fi nerd stuff.

Updated on June 04, 2022

Comments

  • Pete
    Pete almost 2 years

    I've written a backup script for our local dev server (running Ubuntu server edition 9.10), just a simple script to tar & gzip the local root and stick it in a backup folder. It works fine when I run :

    $ bash backups.sh
    

    but it wont work when I run it through crontab.

    59 23 *  *  *  bash /home/vnc/backups/backup.sh >> /home/vnc/backups/backup.log 2> $1
    

    I get the error message

    /bin/sh: cannot create : nonexistent
    

    The script makes the tar.gz in the folder it is running from (/home/user1), but then tries to copy it to a mounted share (/home/backups, which is really 192.168.0.6/backups) from a network drive, via using fstab. The mounted share has permissions 777 but the owner and group are different to those running the script. I'm using bash to run the script instead of sh to get around another issue I've had in the past with "bad substitution" errors

    The first 2 lines of the file are

    ! /bin/bash
    
    cd /home/vnc/backups
    

    I'm probably not supplying enough information to fully answer this post yet but I can post more information as necessary, but I don't really know where to look next.

  • Pete
    Pete about 13 years
    I've already got the cd in the top of the file, I added id >> this.log to the script as the first line but it doesn't even get there far. Where you suggesting I add it to the crontab file?
  • Pete
    Pete about 13 years
    Already got both of those lines in! I'll update the original post
  • Pete
    Pete about 13 years
    Ah wait a sec, I've written "bash" instead of "/bin/bash" in crontab, I'll update to the absolute path and see how that works... ok, that made no difference either
  • SourceSeeker
    SourceSeeker about 13 years
    @Comcar: Ah, I now see where your problem is. There's no contents for $1 that you're sending your standard output to in the crontab entry. You need to specify a filename. It makes no sense to use $1. It's meaningless in that context. Also, in your edit, you have the shebang as "! /bin/bash". It needs to be #! /bin/bash.
  • SourceSeeker
    SourceSeeker about 13 years
    @Comcar: If you mean for stderr to go to the same place as stdout, then it needs to be 2>&1 instead of "2> $1".
  • Pete
    Pete about 13 years
    Sorry yes I do have the #!, just didn't copy the full line. And I can't believe it's just a typo after all that. The crontab wasn't running because of the $/& error. What a fool I've been. Thanks Dennis!
  • abhijit
    abhijit over 6 years
    This doesn't really answer the question as to why that error is coming
  • Shusen Yi
    Shusen Yi over 6 years
    His original code has 2>$1 and the explanation was answered by Dennis Williamson.