Expect script fails when running from cron but works when run manually

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a script in Linux that creates a file which once created it executes an Expect script that uploads the created file to an SFTP server. I have this running in cron and for whatever reason the upload always fails, however when I execute the script in the shell the upload is successful. I checked the logs and they're not really telling me where the process is going wrong.



Here's the shell script



#!/bin/bash

cd /home/mysql/paradata/repoupload || exit
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/gldetail.csv /home/mysql/paradata/repoupload 2>/dev/null
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/vgldetail.csv /home/mysql/paradata/repoupload 2>/dev/null




test -f gldetail.csv && ./SAFCO.sh
test -f vgldetail.csv && ./SAFCO.sh


if [ -f vgldetail.csv ]; then filename=vgldetail.csv; fi
if [ -f gldetail.csv ]; then filename=gldetail.csv; fi


if [ $filename == gldetail.csv ];
then
./uploadsafco.exp REPOEX > lastftplogsafco.txt
else
./uploadvafco.exp REPOEX > lastftplogvafco.txt
fi


Here's the expect script



#!/usr/bin/expect -f


set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if $force_conservative
set send_slow 1 .1
proc send ignore arg
sleep .1
exp_send -s -- $arg



set arg1 [lindex $argv 0]

set timeout 30
expect_after
timeout puts "Timeout"; exit 1

spawn sftp not@this
match_max 100000
expect "password: "
send -- "notapasswordr"
expect -exact "r
sftp> "
send -- "cd /usr/data/r1/EDr"
expect "sftp> "
send -- "progressr"
expect -exact "progressr
Progress meter disabledr
sftp> "
send -- "put REPOEXr"
expect "sftp> "
send -- "chmod 777 REPOEXr"
expect "sftp> "
send -- "byer"
expect eof
exit









share|improve this question

















  • 2




    You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
    – glenn jackman
    Jun 28 '17 at 15:11














up vote
0
down vote

favorite












I have a script in Linux that creates a file which once created it executes an Expect script that uploads the created file to an SFTP server. I have this running in cron and for whatever reason the upload always fails, however when I execute the script in the shell the upload is successful. I checked the logs and they're not really telling me where the process is going wrong.



Here's the shell script



#!/bin/bash

cd /home/mysql/paradata/repoupload || exit
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/gldetail.csv /home/mysql/paradata/repoupload 2>/dev/null
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/vgldetail.csv /home/mysql/paradata/repoupload 2>/dev/null




test -f gldetail.csv && ./SAFCO.sh
test -f vgldetail.csv && ./SAFCO.sh


if [ -f vgldetail.csv ]; then filename=vgldetail.csv; fi
if [ -f gldetail.csv ]; then filename=gldetail.csv; fi


if [ $filename == gldetail.csv ];
then
./uploadsafco.exp REPOEX > lastftplogsafco.txt
else
./uploadvafco.exp REPOEX > lastftplogvafco.txt
fi


Here's the expect script



#!/usr/bin/expect -f


set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if $force_conservative
set send_slow 1 .1
proc send ignore arg
sleep .1
exp_send -s -- $arg



set arg1 [lindex $argv 0]

set timeout 30
expect_after
timeout puts "Timeout"; exit 1

spawn sftp not@this
match_max 100000
expect "password: "
send -- "notapasswordr"
expect -exact "r
sftp> "
send -- "cd /usr/data/r1/EDr"
expect "sftp> "
send -- "progressr"
expect -exact "progressr
Progress meter disabledr
sftp> "
send -- "put REPOEXr"
expect "sftp> "
send -- "chmod 777 REPOEXr"
expect "sftp> "
send -- "byer"
expect eof
exit









share|improve this question

















  • 2




    You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
    – glenn jackman
    Jun 28 '17 at 15:11












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a script in Linux that creates a file which once created it executes an Expect script that uploads the created file to an SFTP server. I have this running in cron and for whatever reason the upload always fails, however when I execute the script in the shell the upload is successful. I checked the logs and they're not really telling me where the process is going wrong.



Here's the shell script



#!/bin/bash

cd /home/mysql/paradata/repoupload || exit
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/gldetail.csv /home/mysql/paradata/repoupload 2>/dev/null
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/vgldetail.csv /home/mysql/paradata/repoupload 2>/dev/null




test -f gldetail.csv && ./SAFCO.sh
test -f vgldetail.csv && ./SAFCO.sh


if [ -f vgldetail.csv ]; then filename=vgldetail.csv; fi
if [ -f gldetail.csv ]; then filename=gldetail.csv; fi


if [ $filename == gldetail.csv ];
then
./uploadsafco.exp REPOEX > lastftplogsafco.txt
else
./uploadvafco.exp REPOEX > lastftplogvafco.txt
fi


Here's the expect script



#!/usr/bin/expect -f


set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if $force_conservative
set send_slow 1 .1
proc send ignore arg
sleep .1
exp_send -s -- $arg



set arg1 [lindex $argv 0]

set timeout 30
expect_after
timeout puts "Timeout"; exit 1

spawn sftp not@this
match_max 100000
expect "password: "
send -- "notapasswordr"
expect -exact "r
sftp> "
send -- "cd /usr/data/r1/EDr"
expect "sftp> "
send -- "progressr"
expect -exact "progressr
Progress meter disabledr
sftp> "
send -- "put REPOEXr"
expect "sftp> "
send -- "chmod 777 REPOEXr"
expect "sftp> "
send -- "byer"
expect eof
exit









share|improve this question













I have a script in Linux that creates a file which once created it executes an Expect script that uploads the created file to an SFTP server. I have this running in cron and for whatever reason the upload always fails, however when I execute the script in the shell the upload is successful. I checked the logs and they're not really telling me where the process is going wrong.



Here's the shell script



#!/bin/bash

cd /home/mysql/paradata/repoupload || exit
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/gldetail.csv /home/mysql/paradata/repoupload 2>/dev/null
mv /mnt/restrictedreports/Restricted/Corporate/Accounting/GL2013/vgldetail.csv /home/mysql/paradata/repoupload 2>/dev/null




test -f gldetail.csv && ./SAFCO.sh
test -f vgldetail.csv && ./SAFCO.sh


if [ -f vgldetail.csv ]; then filename=vgldetail.csv; fi
if [ -f gldetail.csv ]; then filename=gldetail.csv; fi


if [ $filename == gldetail.csv ];
then
./uploadsafco.exp REPOEX > lastftplogsafco.txt
else
./uploadvafco.exp REPOEX > lastftplogvafco.txt
fi


Here's the expect script



#!/usr/bin/expect -f


set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if $force_conservative
set send_slow 1 .1
proc send ignore arg
sleep .1
exp_send -s -- $arg



set arg1 [lindex $argv 0]

set timeout 30
expect_after
timeout puts "Timeout"; exit 1

spawn sftp not@this
match_max 100000
expect "password: "
send -- "notapasswordr"
expect -exact "r
sftp> "
send -- "cd /usr/data/r1/EDr"
expect "sftp> "
send -- "progressr"
expect -exact "progressr
Progress meter disabledr
sftp> "
send -- "put REPOEXr"
expect "sftp> "
send -- "chmod 777 REPOEXr"
expect "sftp> "
send -- "byer"
expect eof
exit






linux shell cron expect






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 28 '17 at 13:20









Joseph Sortino

1225




1225







  • 2




    You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
    – glenn jackman
    Jun 28 '17 at 15:11












  • 2




    You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
    – glenn jackman
    Jun 28 '17 at 15:11







2




2




You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
– glenn jackman
Jun 28 '17 at 15:11




You're not specifying the full path to sftp -- is it available in cron's PATH for the user?
– glenn jackman
Jun 28 '17 at 15:11










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










"Works manually, fails in cron" is almost always due to one of these:



  • differences in environment variables: PATH and others;

  • different current working directory;

  • lack of a TTY (probably not an issue with expect scripts);

  • permissions (interactive testing with one user, cron job with another); or

  • different shell: commands executed in within the cron command line itself use one shell, you may be interactively using another.





share|improve this answer



























    up vote
    0
    down vote













    I have similar problem with my spawn telnet script. Script work fine until adding expect_after for handle timeout.
    Try remove expect_after .. . It's work for me.






    share|improve this answer



























      up vote
      0
      down vote













      My question had just been marked as a copy of this one, but I still don't get the answer, why is the crontab line



      30 14 10 10 * /usr/bin/expect -f /root/rutmain.sh


      Not working when I try to run a expect script if it runs perfectly in shell, I get no error from the cron log, it just DON'T RUN.






      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f373922%2fexpect-script-fails-when-running-from-cron-but-works-when-run-manually%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        "Works manually, fails in cron" is almost always due to one of these:



        • differences in environment variables: PATH and others;

        • different current working directory;

        • lack of a TTY (probably not an issue with expect scripts);

        • permissions (interactive testing with one user, cron job with another); or

        • different shell: commands executed in within the cron command line itself use one shell, you may be interactively using another.





        share|improve this answer
























          up vote
          3
          down vote



          accepted










          "Works manually, fails in cron" is almost always due to one of these:



          • differences in environment variables: PATH and others;

          • different current working directory;

          • lack of a TTY (probably not an issue with expect scripts);

          • permissions (interactive testing with one user, cron job with another); or

          • different shell: commands executed in within the cron command line itself use one shell, you may be interactively using another.





          share|improve this answer






















            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            "Works manually, fails in cron" is almost always due to one of these:



            • differences in environment variables: PATH and others;

            • different current working directory;

            • lack of a TTY (probably not an issue with expect scripts);

            • permissions (interactive testing with one user, cron job with another); or

            • different shell: commands executed in within the cron command line itself use one shell, you may be interactively using another.





            share|improve this answer












            "Works manually, fails in cron" is almost always due to one of these:



            • differences in environment variables: PATH and others;

            • different current working directory;

            • lack of a TTY (probably not an issue with expect scripts);

            • permissions (interactive testing with one user, cron job with another); or

            • different shell: commands executed in within the cron command line itself use one shell, you may be interactively using another.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 24 '17 at 19:25









            Kaz

            4,45411431




            4,45411431






















                up vote
                0
                down vote













                I have similar problem with my spawn telnet script. Script work fine until adding expect_after for handle timeout.
                Try remove expect_after .. . It's work for me.






                share|improve this answer
























                  up vote
                  0
                  down vote













                  I have similar problem with my spawn telnet script. Script work fine until adding expect_after for handle timeout.
                  Try remove expect_after .. . It's work for me.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I have similar problem with my spawn telnet script. Script work fine until adding expect_after for handle timeout.
                    Try remove expect_after .. . It's work for me.






                    share|improve this answer












                    I have similar problem with my spawn telnet script. Script work fine until adding expect_after for handle timeout.
                    Try remove expect_after .. . It's work for me.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 8 at 4:32









                    user289817

                    1




                    1




















                        up vote
                        0
                        down vote













                        My question had just been marked as a copy of this one, but I still don't get the answer, why is the crontab line



                        30 14 10 10 * /usr/bin/expect -f /root/rutmain.sh


                        Not working when I try to run a expect script if it runs perfectly in shell, I get no error from the cron log, it just DON'T RUN.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          My question had just been marked as a copy of this one, but I still don't get the answer, why is the crontab line



                          30 14 10 10 * /usr/bin/expect -f /root/rutmain.sh


                          Not working when I try to run a expect script if it runs perfectly in shell, I get no error from the cron log, it just DON'T RUN.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            My question had just been marked as a copy of this one, but I still don't get the answer, why is the crontab line



                            30 14 10 10 * /usr/bin/expect -f /root/rutmain.sh


                            Not working when I try to run a expect script if it runs perfectly in shell, I get no error from the cron log, it just DON'T RUN.






                            share|improve this answer












                            My question had just been marked as a copy of this one, but I still don't get the answer, why is the crontab line



                            30 14 10 10 * /usr/bin/expect -f /root/rutmain.sh


                            Not working when I try to run a expect script if it runs perfectly in shell, I get no error from the cron log, it just DON'T RUN.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 13 mins ago









                            Alejandro Enrique Garcia Navar

                            42




                            42



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f373922%2fexpect-script-fails-when-running-from-cron-but-works-when-run-manually%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay