简体   繁体   中英

Ubuntu: Starting a shell script with args at boot

I'm running Ubuntu Server 14.04 and I'm trying to get a shell script to run at boot. The problem is the script requires args, one of which is a file (a database) and the other is a port number which is found in the same folder as the script. I'm fairly new to this.

When I go in the folder in terminal for example, I can type:

sh script.sh potato 1234

script.sh is the script, potato is the filename, and 1234 is the port number. Works fine when I do it manually.

I tried adding a crontab, @reboot script.sh potato 1234, of course it didn't work, it couldn't find the script.

So I tried:

 @reboot */path/to/my/script.sh* potato 1234

again, didn't work. Figured it couldn't find the database.

So I tried:

@reboot path/to/my/script.sh /path/to/my/potato 1234

Still no dice.

I tried running it in term with paths as well

sh path/to/my/script.sh potato 1234

of course failed - the script up and told me it couldn't find the db, as it should.

sh /path/to/my/script.sh /path/to/my/potato 1234 returned no errors, but it didn't start either.

This is what the script I'm trying to start looks like:

if [ $# -lt 1 -o $# -gt 2 ]; then
    echo 'Usage: restart dbase-prefix [port]'
    exit 1
fi

if [ ! -r $1.db ]; then
    echo "Unknown database: $1.db"
    exit 1
fi

if [ -r $1.db.new ]; then
    mv $1.db $1.db.old
    mv $1.db.new $1.db
    rm -f $1.db.old.Z
    compress $1.db.old &
fi

if [ -f $1.log ]; then
    cat $1.log >> $1.log.old
    rm $1.log
fi

echo `date`: RESTARTED >> $1.log
nohup ./moo $1.db $1.db.new $2 >> $1.log 2>&1 &``

Any clues?

You need to see the output of your program. cron mails the output to the owner of the crontab or to the address specified in the MAILTO environment variable, on top of the crontab.

Be careful about execution environment. Typically, most of the basic environment variables ( PATH , HOME , etc.) will not be set, and might lead to execution errors.

For more information, see the cron man page:

man 5 crontab

These posts might also help you :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM