Expect script does not work under crontab

后端 未结 4 917
挽巷
挽巷 2020-12-29 08:22

I have an expect script which I need to run every 3 mins on my management node to collect tx/rx values for each port attached to DCX Brocade SAN Switch usin

相关标签:
4条回答
  • 2020-12-29 08:36

    I had the same issue, except that my script was ending with

    interact
    

    Finally I got it working by replacing it with these two lines:

    expect eof
    exit
    
    0 讨论(0)
  • 2020-12-29 08:36

    There are two key differences between a program that is run normally from a shell and a program that is run from cron:

    1. Cron does not populate (many) environment variables. Notably absent are TERM, SHELL and HOME, but that's just a small proportion of the long list that will be not defined.
    2. Cron does not set up a current terminal, so /dev/tty doesn't resolve to anything. (Note, programs spawned by Expect will have a current terminal.)

    With high probability, any difficulties will come from these, especially the first. To fix, you need to save all your environment variables in an interactive session and use these in your expect script to repopulate the environment. The easiest way is to use this little expect script:

    unset -nocomplain ::env(SSH_AUTH_SOCK)   ;# This one is session-bound anyway
    puts [list array set ::env [array get ::env]]
    

    That will write out a single very long line which you want to put near the top of your script (or at least before the first spawn). Then see if that works.

    0 讨论(0)
  • 2020-12-29 08:37

    Changing interact to expect eof worked for me!

    Needed to remove the exit part, because I had more statements in the bash script after the expect line (calling expect inside a bash script).

    0 讨论(0)
  • 2020-12-29 08:39

    Jobs run by cron are not considered login shells, and thus don't source your .bashrc, .bash_profile, etc.

    If you want that behavior, you need to add it explicitly to the crontab entry like so:

    $ crontab -l
    0 13 * * * bash -c '. .bash_profile; etc ...'
    $
    
    0 讨论(0)
提交回复
热议问题