0

I created an expect script that won't seem to run when called by cron. I included the full path in cron when calling it as follows.

  *   *   *   *   *     /usr/local/sbin/checkForRemoteTasks >/tmp/checkForRemoteTasks.output

This calls the script checkForRemoteTasks and exports the results to /tmp/checkForRemoteTasks.output. The contents of the script are as follows:

#!/usr/bin/expect -f
set timeout -1
set env(TERM) vt100

if [file exists "/usr/local/sbin/remoteTasks/restartMySQL"] {
    ## STOP APACHE FIRST
    spawn apache2ctl stop
    expect "#"
    spawn service mysql restart
    expect "#"
    spawn rm /usr/local/sbin/remoteTasks/restartMySQL
    expect "#"
    spawn apache2ctl start
    expect "#"
}
if [file exists "/usr/local/sbin/remoteTasks/restartApache"] {
    spawn apache2ctl graceful
    expect "#"
    spawn rm /usr/local/sbin/remoteTasks/restartApache
    expect "#"
}

I have a script on my test server that uploads a file to the /usr/local/sbin/remoteTasks/restartMySQL file. The file contains one character. The expect script ran by cron (on my production server) should check for the restartMySQL file and if it finds the file, it should spawn the appropriate commands to stop apache, restart MySQL, and then restart apache. It works when I run it manually but not when ran through cron. Any help would be appreciated.

Here is the output of /tmp/checkForRemoteTasks.output:

spawn apache2ctl stop
  • Most common problem with running scripts in cron is that cron runs with a minimal shell and minimal environment, so use the full path to all executable (expect, service) and files. – Panther Aug 16 '14 at 0:32
0

Are any of those commands actually interactive? If no, just use exec

if {[file exists "/usr/local/sbin/remoteTasks/restartMySQL"]} {
    ## STOP APACHE FIRST
    exec apache2ctl stop
    exec service mysql restart
    file delete /usr/local/sbin/remoteTasks/restartMySQL
    exec apache2ctl start
}
if {[file exists "/usr/local/sbin/remoteTasks/restartApache"]} {
    exec apache2ctl graceful
    file delete /usr/local/sbin/remoteTasks/restartApache
}

But if you're doing that, you might was well stick with shell:

#!/bin/sh
if [[ -f /usr/local/sbin/remoteTasks/restartMySQL ]]; then
    ## STOP APACHE FIRST
    apache2ctl stop
    service mysql restart
    rm /usr/local/sbin/remoteTasks/restartMySQL
    apache2ctl start
fi
if [[ -f /usr/local/sbin/remoteTasks/restartApache ]]; then
    apache2ctl graceful
    rm /usr/local/sbin/remoteTasks/restartApache
fi
  • Thanks Glenn! This works. I don't know bash scripting well. I keep using the same expect script and modify it to make all my scripts. – CEOofOGN Aug 16 '14 at 3:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.