Announcement

Collapse
No announcement yet.

[SOLVED + HOW-TO] ImageMagick import with cron

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

    [SOLVED + HOW-TO] ImageMagick import with cron

    I am having some problems with scheduling import with Cron. I wrote a shell script to take a screenshot with import and store it in a hidden directory to be out of the way until the images are needed. The script works flawlessly on it's own, but when I schedule it in Cron, the new screenshots never show up. Cron is working just fine, so that's not the problem. Here's the original script:

    Code:
    #!/bin/bash
    # Takes a screenshot with ImageMagick.
    # Screenshot will save to ~/.grabber/
    
    # The name of the file
    screenshot='grab';
    
    # Adds the date and time to the filename
    nano=`date '+%Y%m%d%H%M%S'`;
    
    # Adds the file extension
    extension='.jpg';
    
    # Generate a filename
    file="$HOME/.grabber/$screenshot-$nano$extension";
    
    # Use ImageMagick to take the screenshot
    import -window root -quiet -geometry 640x400 -silent $file;
    I saved this to /usr/bin/grabber and made it executable. Like I said, it works fine on it's own, even when I just type "grabber" into the run dialog or a terminal.

    In crontab, I set it up like this:
    Code:
    * * * * * /usr/bin/grabber
    (It's set to run every minute only for testing until I get it working)

    I've tried several variations of the script, including condensing it into a static command ("/usr/bin/import -window root -quiet -geometry 640x400 -silent /home/<USERNAME>/.grabber/`date '+%Y%m%d%H%M%S'`.jpg" and "/usr/bin/import -window root -quiet -geometry 640x400 -silent /home/<USERNAME>/.grabber/`date`.jpg") both in the script and inserting it directly into crontab instead of telling it to run the script. I would much rather the dynamic script, but whatever works. I have also tried putting it in /etc/crontab as "* * * * * <USERNAME> /usr/bin/grabber", but that still didn't work.

    Any advice?
    Keyboard not detected. Press F1 to continue...

    #2
    Re: ImageMagick import with cron

    Did you try redirecting stdout/stderr to a file to see if import pukes out an error message when started by the cron daemon?

    Comment


      #3
      Re: ImageMagick import with cron

      Also, I wonder what $HOME evaluates to in the case of a cron job?

      Comment


        #4
        Re: ImageMagick import with cron

        Nevermind, I figured it out. I needed to add the "DISPLAY" and "HOME" variables to the script. Here's what the script looks like now (might come in useful for others):
        Code:
        #!/bin/bash
        DISPLAY=:0
        export DISPLAY
        HOME=/home/<USERNAME>
        export HOME
        # Takes a screenshot with ImageMagick.
        # Screenshot will save to ~/.grabber/
        #
        # The name of your file
        screenshot='grab';
        #
        # Adds the date and time to filename to prevent overwriting
        nano=`date '+%Y%m%d%H%M%S'`;
        #
        # Adds the file extension
        extension='.jpg';
        #
        # Generate a filename
        file="$HOME/.grabber/$screenshot-$nano$extension";
        #
        # Use ImageMagick to take the screenshot
        /usr/bin/import -window root -quiet -geometry 640x400 -silent $file;
        With the crontab entry (personal, not system) looking like this:
        Code:
        * * * * * /usr/bin/grabber
        So here's a quick how-to:
        • Create the storage directory that you want the screenshots to go to.

        Code:
        mkdir $HOME/.grabber
        • Copy the grabber script into kate or whatever text editor you prefer.
        • Edit the script: change <USERNAME> to your username, change the name if you want (screenshot='grab'), change date format if you want (see "man date" for options), change import options to your liking (resolution, etc)
        • Save it and make it executable. In Konqueror or Dolphin, right click > Properties > Permissions > Mark "Is Executable"
        • Copy or move grabber to /usr/bin

        Code:
        sudo mv grabber /usr/bin/grabber
        At this point, if you run "grabber", it will take a screenshot and store it. If you want to add it to cron:
        • Open your personal crontab:

        Code:
        crontab -e
        • Choose your editor if needed.
        • Add the entry:

        Code:
        * * * * * /usr/bin/grabber
        This will create a new screenshot every minute. If you want it to be done every 5 minutes, you would want to change it to "*/5 * * * * /usr/bin/grabber". See "man cron" for more information on the "when" settings.

        If you don't want the script in /usr/bin/, you can keep it in whatever directory you want (eg: $HOME/.grabber and create $HOME/.grabber/grabs to store the screenshots and edit the script appropriately)

        You can also remove the DISPLAY and HOME variables from the script
        Code:
        DISPLAY=:0
        export DISPLAY
        HOME=/home/<USERNAME>
        export HOME
        and put it into your personal crontab
        Code:
        $ crontab -e
        
        DISPLAY=:0
        HOME=/home/<USERNAME>
        
        * * * * * /usr/bin/grabber
        Keyboard not detected. Press F1 to continue...

        Comment


          #5
          Re: [SOLVED + HOW-TO] ImageMagick import with cron

          Yes, you need to set DISPLAY (Maybe the script should check on what display the user is logged in, it might not always be :0?), but why do you need to set HOME?

          Comment


            #6
            Re: [SOLVED + HOW-TO] ImageMagick import with cron

            Originally posted by kubicle
            Yes, you need to set DISPLAY (Maybe the script should check on what display the user is logged in, it might not always be :0?)...
            Good point, thanks for mentioning it. However, I don't know how to go about having it check. Do you have any advice on this?

            Originally posted by kubicle
            ... but why do you need to set HOME?
            Because the script calls for "$HOME/.grabber...." and if it's not specified somewhere, cron simply doesn't know how to handle that variable. I've tried with and without HOME being set, and it wouldn't work without. It's just too bad you can't set a variable with a variable to make it even more dynamic (HOME=/user/$USER).

            Also, I personally think it would be better to set these variables in crontab rather than in the script in case it gets ran as a different user or other possible problems like sharing the script between computers.
            Keyboard not detected. Press F1 to continue...

            Comment


              #7
              Re: [SOLVED + HOW-TO] ImageMagick import with cron

              Originally posted by digitalhead
              Originally posted by kubicle
              Yes, you need to set DISPLAY (Maybe the script should check on what display the user is logged in, it might not always be :0?)...
              Good point, thanks for mentioning it. However, I don't know how to go about having it check. Do you have any advice on this?
              This is just one option to go about it, I'm sure someone could cook up something elegant:
              DISPLAY=$(who | sort | grep -m1 "^$LOGNAME" | awk '{print $2}')
              (This should set DISPLAY as the lowest number X session the user is logged in)

              Originally posted by digitalhead
              Originally posted by kubicle
              ... but why do you need to set HOME?
              Because the script calls for "$HOME/.grabber...." and if it's not specified somewhere, cron simply doesn't know how to handle that variable.
              Hmm...I tried it and worked fine without setting HOME (although I used /etc/crontab with username field set)...also, this is a quote from 'man 5 crontab':
              SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.
              (So it probably *should* work without setting HOME explicitly)

              Comment


                #8
                Re: [SOLVED + HOW-TO] ImageMagick import with cron

                Originally posted by kubicle
                ...
                This is just one option to go about it, I'm sure someone could cook up something elegant:
                DISPLAY=$(who | sort | grep -m1 "^$LOGNAME" | awk '{print $2}')
                (This should set DISPLAY as the lowest number X session the user is logged in)
                ...
                I'll have to give that a try. I've been working on this little project for the better part of 2 days now, so I'll probably play with it more later.

                Originally posted by kubicle
                ...
                Hmm...I tried it and worked fine without setting HOME (although I used /etc/crontab with username field set)...also, this is a quote from 'man 5 crontab':
                SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner.
                (So it probably *should* work without setting HOME explicitly)
                That's interesting. I don't think I did anything to cause problems with that in my process of trying to get it working. I'll have to check into that as well, especially since it seems to be a problem on my system.

                Thank you for all of your input kubicle, it is truly appreciated. I've been using Linux for about 8 years now (I think) and this has been my first real dive into cron with a script like this. It's been a little rough at times, but still an enjoyable and educational experience, which the educational part is one thing I was hoping for.
                Keyboard not detected. Press F1 to continue...

                Comment

                Working...
                X