Announcement

Collapse
No announcement yet.

problems capturing script output

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    problems capturing script output

    Hi all,
    So I want to have a script, that I'm using Webmin to run, capture the output of the script into a file that I can then have emailed to myself.

    When I tried adding exec &> capture.txt to the top of the script, I got a permission denied error about creating the file. I dont really understand why this is happening, my only idea is that the directory the file is trying to be created in is not something that can be written to? But I dont understand where the file was going to be written to....

    Any guidance?
    System Information<br />Distro: Ubuntu 11.04<br />KDE: Platform Version 4.6.2<br />Grub: 0.97-29ubuntu61.1 GRand Unified Bootloader (Legacy version)<br /><br />PC Hardware:<br />laptop HP Pavilion dv6<br />CPU: AMD Turion(tm) II P520 Dual-Core Processor<br />GPU: ATI Technologies Inc M880G [Mobility Radeon HD 4200]

    #2
    Re: problems capturing script output

    I overcame that problem by switching the owner of the cronjob to root, but now I can't find where the capture.txt file was created.
    System Information<br />Distro: Ubuntu 11.04<br />KDE: Platform Version 4.6.2<br />Grub: 0.97-29ubuntu61.1 GRand Unified Bootloader (Legacy version)<br /><br />PC Hardware:<br />laptop HP Pavilion dv6<br />CPU: AMD Turion(tm) II P520 Dual-Core Processor<br />GPU: ATI Technologies Inc M880G [Mobility Radeon HD 4200]

    Comment


      #3
      Re: problems capturing script output

      Open a console and type:
      Code:
      sudo find / -name capture.txt
      Windows no longer obstructs my view.
      Using Kubuntu Linux since March 23, 2007.
      "It is a capital mistake to theorize before one has data." - Sherlock Holmes

      Comment


        #4
        Re: problems capturing script output

        after finding the file I can see that exec &> capture.txt only created that file, it did not copy the command output into the file. Any suggestions as to how to actually grab the command output?
        System Information<br />Distro: Ubuntu 11.04<br />KDE: Platform Version 4.6.2<br />Grub: 0.97-29ubuntu61.1 GRand Unified Bootloader (Legacy version)<br /><br />PC Hardware:<br />laptop HP Pavilion dv6<br />CPU: AMD Turion(tm) II P520 Dual-Core Processor<br />GPU: ATI Technologies Inc M880G [Mobility Radeon HD 4200]

        Comment


          #5
          Re: problems capturing script output

          Originally posted by erinsaurus87
          after finding the file I can see that exec &> capture.txt only created that file, it did not copy the command output into the file. Any suggestions as to how to actually grab the command output?
          Does your script give normal output (to STDOUT) or error output (to STDERR)?

          Code:
          exec & > capture.txt
          Will only write STDOUT to capture.txt, if you wish to catch both outputs, you can try:

          Code:
          exec & > capture.txt 2>&1
          this should output STDERR to the same place as STDOUT, if you only wish to catch STDERR, you can try:
          Code:
          exec & 2> capture.txt
          Note: You can also use full path to write the output file to a location of your choosing (possibly somewhere that your user has write access to):
          Code:
          exec & > /path/to/capture.txt 2>&1
          If redirecting STDERR won't help, could you post the script for review?

          EDIT: btw, you can also redirect from the cronjob (instead of from inside the script). This way you'd get the output normally in the terminal if you run the script manually...and get cronjob execution output to a file, example:
          1 8 * * * root yourscript > /path/to/capture.txt 2>&1

          Comment


            #6
            Re: problems capturing script output

            Well I tried this and the capture.txt file was not created in the directory:
            # * * * * 5 /var/www/SocProjects_testing/cronJobs/trial_fullbackup.sh > /home/erin/capture.txt 2>&1

            And I tried putting something in the script itself and while it creates a capture.txt file, it has no output in it. This is my script:
            Code:
            [code]exec & > capture.txt 2>&1
            # WEEKLY FRIDAY AFTERNOON BASELINE FULL BACKUP RUNS AT 7PM
            # script for SOC projects filesystem/code and mysql database, to be executed weekly.
            #################################
            a=$(date +%H:%M--%d_%m_%Y)
            b=$(date +%m_%d_%Y)
            #################################
            echo "deleting the following directories:"
            find /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/ -maxdepth 1 -mtime +1 -exec ls {} \;
            find /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/ -maxdepth 1 -mtime +1 -exec rm -rf {} \;
            #echo "backup process complete"
            ###################################################################
            echo "mkdir /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/".$b
            mkdir /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/$b/
            ################################
            echo "dump socProjects sql database"
            mysqldump socProjects > /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/$b/socProjects.$b.sql
            ###############################
            chown -Rf erin /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/$b
            ###############################
            cd /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/$b/
            tar -g fullbackup.snar -c -f projbackup.$b.tar /home/erin/tartest
            cp fullbackup.snar /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups
            ##############################
            cd /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/
            echo "tar backup directory".$b
            tar cfz socprojects.$b.tar /home/erin/Documents/SOC/ProjectsDatabase/socprojects_backups/$b
            echo "scp backup directory to remote server"
            scp socprojects.$b.tar erind@remote:/home/erind/socprojects_backups
            echo "delete copy of backup directory.tar on local server"
            rm socprojects.$b.tar
            #################################
            [/code]
            System Information<br />Distro: Ubuntu 11.04<br />KDE: Platform Version 4.6.2<br />Grub: 0.97-29ubuntu61.1 GRand Unified Bootloader (Legacy version)<br /><br />PC Hardware:<br />laptop HP Pavilion dv6<br />CPU: AMD Turion(tm) II P520 Dual-Core Processor<br />GPU: ATI Technologies Inc M880G [Mobility Radeon HD 4200]

            Comment


              #7
              Re: problems capturing script output

              Originally posted by erinsaurus87
              Well I tried this and the capture.txt file was not created in the directory:
              # * * * * 5 /var/www/SocProjects_testing/cronJobs/trial_fullbackup.sh > /home/erin/capture.txt 2>&1
              Did the job (and the script) actually run?

              A few quick notes on that:
              1. you did remove the '#' in front of the line (otherwise the cronjob is commented out)?
              2. "* * * * 5" will run the job every minute on fridays, probably not what you want?
              3. which crontab did you put this to (systemwide /etc/crontab needs a username entry in front of the command)?

              Code:
              exec & > capture.txt 2>&1
              Does it work if you remove the run-in-background '&'?
              Code:
              exec > /you/can/use/a/path/here/as/well/capture.txt 2>&1

              Comment


                #8
                Re: problems capturing script output

                It does work when I remove the run-in-background '&'.

                Thanks!
                System Information<br />Distro: Ubuntu 11.04<br />KDE: Platform Version 4.6.2<br />Grub: 0.97-29ubuntu61.1 GRand Unified Bootloader (Legacy version)<br /><br />PC Hardware:<br />laptop HP Pavilion dv6<br />CPU: AMD Turion(tm) II P520 Dual-Core Processor<br />GPU: ATI Technologies Inc M880G [Mobility Radeon HD 4200]

                Comment

                Working...
                X