create an empty file every minute


5 posts
by tomexx » Mon Dec 17, 2012 6:28 pm
Hi,
Trying to create a empty files every minute using crontab but it's not working.
I would like to use a time stamp for a file name to get this:

10-24-33.jpg
10-25-33.jpg
10-26-33.jpg

Tried the following two ideas:
/1 * * * * touch $( date '+%H-%M-%S' )

* * * * * touch $( date '+%H-%M-%S' )

Thanks,
Tom
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth
by efflandt » Mon Dec 17, 2012 7:30 pm
Since cron runs with minimal environment, assume no environment and use full paths. It could be that the user you ran crontab -e as does not have permission to write to whatever unknown current directory cron runs from. So you should use /usr/bin/touch and full path to where that user has permission to save the file.

Note that constantly touching a file on SD will result in wearing its flash memory. So when testing how long a rechargable battery pack would last for the Pi, I wrote a pair of client/server python scripts. The client script on the Pi grabs /proc/uptime every 5 minutes (adjustable) and sends localtime and calculated uptime to the server script on my Linux desktop PC which logs it.

The scripts are also useful to test network reliability of the Pi. During a current test, after over 10 hrs the Pi momentarily lost avahi (.local) name resolution for my Linux desktop which stopped the script. But ssh was still connected and client script continued working when restarted. So I need to figure out how to make it retry if it momentarily gets "socket.gaierror: [Errno -2] Name or service not known".
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA
by tomexx » Mon Dec 17, 2012 8:10 pm
Thanks for your help. I'll try /usr/bin/touch.

Where would I specify the path to my output files (/home/pi/)?

/1 * * * * /usr/bin/touch $(/home/pi/ + date '+%H-%M-%S' )
or
/1 * * * * /usr/bin/touch $( date '+/home/pi/ %H-%M-%S' )
or some other place
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth
by efflandt » Tue Dec 18, 2012 3:57 am
efflandt@raspbian ~ $ echo "/home/pi/$( /bin/date '+%H-%M-%S' ).jpg"
/home/pi/20-53-11.jpg


The problem is that cron interprets anything after % to be input to whatever is before that, and not sure how to escape that. So since cron cannot properly interpret your date parameters, it would be better use a short script. Create a bin directory in your home directory, which will automatically be in your $PATH the next time you login, so it is a good place to put personal scripts or binaries. In your ~/bin, create a short script called touchme and then give it execute permission with chmod ug+x touchme

Code: Select all
#!/bin/sh
/usr/bin/touch "/home/pi/$( /bin/date '+%H-%M-%S' ).jpg"


But you may want to make a subdirectory and extend the path into that, so all those files do not jam up your home directory. Then all you need in crontab -e is

Code: Select all
* * * * * ~/bin/touchme


Works for me, but note that seconds will probably never change, since cron is run once per minute maximum, so seconds may be redundant information.
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA
by tomexx » Tue Dec 18, 2012 1:10 pm
Thanks efflandt.
Appreciate the info about script usage and cron as I'm trying to learn cron by doing and getting mixed results.
Tom
Posts: 106
Joined: Thu Nov 01, 2012 4:58 pm
Location: Kitchener, ON, Canada, Earth