Register
It is currently Sat Dec 20, 2014 11:03 pm

Script for a time lapse screenshot programme


All times are UTC - 6 hours


Post new topic Reply to topic  [ 16 posts ] 
Author Message
 PostPosted: Mon Oct 26, 2009 9:12 am   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
I am using "scrot" to take timed screenshots of a shipping channel, to collect the ships that pass through. The terminal programme I use is listed here:

while true;do scrot '%s.png' & sleep 60;done

It works very will and will take screenshots, one minute apart until the disk is full, the power goes off or I shut it down with Ctl C. I would love to have a bash script where I could limit the number of screenshoots. For example if I set if for 400, it will take a screenshot, once every minute for 400 cycles.

As it is daylight where the webcam is we are sleeping where I live. Is there a way for the bash script to include a command to shutdown the computer?

Any help with this would be really appreciated.

Thank you.


Top
 Profile  
 PostPosted: Tue Oct 27, 2009 3:17 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Code:
numberofloops=400
for iloop in $(seq 1 $numberofloops); do
  scrot '%s.png
  sleep 60
done


to have the computer shutdown the command will either need to be run as root or the user this runs as will need passwordless sudo access to a shutdown command. Depending on your distro this may be as easy as adding that user to a specific 'admin' or 'wheel' group.

sudo poweroff

I hope that helps a little :)
DW


Top
 Profile  
 PostPosted: Tue Oct 27, 2009 8:42 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
Hi Darthwavy,

Thank you for your reply. I ran what you sent as follows:

numberofloops=10;for iloop in $(seq 1 $numberofloops);do scrot '%s.png sleep 60;done

Seems I've got something wrong as it didn't work. You are dealing with an aging newbie here... :)

Can I put this in a file which will execute like the old batch files?

Best wishes, Elder73


Top
 Profile  
 PostPosted: Wed Oct 28, 2009 4:03 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
That won't work... First of all there's a missing semicolon after .png
And I'm puzzled by the "%s.png" part.

Something like this would do the trick I believe
Code:
numberofloops=10; for i in $(seq -w 1 $numberofloops); do scrot "$(date +%Y-%m-%d)_$i.png"; sleep 60; done


This will name the files as follows (if $numberofloops was set to 100):
2009-10-28_001.png
2009-10-28_002.png
....
2009-10-28_099.png
2009-10-28_100.png

And yes, you can put this in a file, just add the shabang at the top of the file (#!/bin/bash that is) and then run
~# chmod +x file.sh
To make it executable.

But I would recommend for readability that you do the script file like this instead:
Code:
#!/bin/bash
numberofloops=10
for i in $(seq -w 1 $numberofloops); do
    date=$(date +%Y-%m-%d)
    scrot "$date_$i.png"
    sleep 60
done

Both versions are working examples, it's just 2 different ways of doing it. If you're creating a script then the second version is better when you're going to maintain it in the future.

If you're just gonna run the line then the one-liner suffices :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Oct 28, 2009 2:53 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
Thank you Frederik, the bash script worked fine.

For "poweroff" to work, I was told previously that it can only be done as root. Is it possible to add something to the script like:

su
my password
poweroff

What I capture is caught when I am in bed as it is daylight there when it is dark here.

Bill Bradley (elder73)


Top
 Profile  
 PostPosted: Thu Oct 29, 2009 2:22 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
You were told that SUDO might solve this issue for you.

"su" does not support any feature where you can password-less execute a command.

This would work thou if you have sudo set up properly.
Code:
# sudo shutdown


What you should do is add a line in your /etc/sudoers file that looks something like this:
Code:
username username = NOPASSWD: /usr/bin/shutdown

Where username is the user that shall execute the sudo command.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Oct 29, 2009 5:47 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
Thank you Fredrik, I will do as you suggest. Just as soon as I read up on how to open the sudoers file. I can wander around a bit in terminal but that is about the limit at the moment. This final piece will finish off the script nicely. I had it running last night, took 420 shots and got five images that I wanted to keep. So far, the wonderful thing about linux is that you can nearly always find someone who will lend you a helping hand. I have had to work through a few problems and all of them are fixed now.

Regards,

Bill Bradley.


Top
 Profile  
 PostPosted: Fri Oct 30, 2009 4:50 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
use the VISUDO command to edit sudoers
only root can issue the visudo command


Top
 Profile  
 PostPosted: Fri Oct 30, 2009 4:55 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
Thank you Frederik, I added the following line to the /etc/sudoers file.
Thank you DarthWavy for the VISUDO tip.

username elder73 = NOPASSWD: /usr/bin/shutdown

Are the spaces affecting anything? The sudoers file has things sort of jammed together.

When I ran it, the following was the result which, of course, did not shut anything down because it was looking for a password:

[elder73@localhost ~]$ numberofloops=5; for i in $(seq -w 1 $numberofloops); do scrot "$(date
+%Y-%m-%d)_$i.png"; sleep 60; done; sudo shutdown
Password:

I seem to have gone amiss somewhere. :)

Best Wishes,

Bill Bradley


Top
 Profile  
 PostPosted: Sun Nov 01, 2009 10:11 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
elder73 wrote:
Thank you Frederik, I added the following line to the /etc/sudoers file.
Thank you DarthWavy for the VISUDO tip.

username elder73 = NOPASSWD: /usr/bin/shutdown

Are the spaces affecting anything? The sudoers file has things sort of jammed together.

When I ran it, the following was the result which, of course, did not shut anything down because it was looking for a password:

[elder73@localhost ~]$ numberofloops=5; for i in $(seq -w 1 $numberofloops); do scrot "$(date
+%Y-%m-%d)_$i.png"; sleep 60; done; sudo shutdown
Password:

Bill Bradley


This was not as clear as it could have been in the above message:

I ran the following and it worked perfectly:

[elder73@localhost ~]$ numberofloops=5; for i in $(seq -w 1 $numberofloops); do scrot "$(date
+%Y-%m-%d)_$i.png"; sleep 60; done; sudo shutdown

Except for the last command, it did not shutdown the computer but asked for a:

Password:

Help is appreciated.


Top
 Profile  
 PostPosted: Mon Nov 02, 2009 3:25 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
i believe the line should say:
elder73 elder73 = NOPASSWD: /sbin/shutdown

But not entirely sure, sudoers never been a good friend of mine ;P and i'm not sure where shutdown binary is located.
You can find out where it is by doing a "which shutdown" or "whereis shutdown".

There's lots of documentation about using sudoers if you just google for it :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Nov 02, 2009 1:32 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
fredrik.eriksson wrote:
i believe the line should say:
elder73 elder73 = NOPASSWD: /sbin/shutdown

But not entirely sure, sudoers never been a good friend of mine ;P and i'm not sure where shutdown binary is located.
You can find out where it is by doing a "which shutdown" or "whereis shutdown".

There's lots of documentation about using sudoers if you just google for it :)


Thank you Fredrik. The shutdown is in: /usr/bin/shutdown and /sbin/shutdown

I have no idea why it is in two places.

I have been googling for sudoers and found a man page for it last night. When I opened it most of the information sailed right over my bald head. :) However, toward the end there was some material that was in keeping with what I am trying to do. The man page gave this example:

ray rashmore = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm

The explanation given was that "ray" on machine "rashmore" could act as root on the directories given.
So I guess I will have to experiment a little more.


Top
 Profile  
 PostPosted: Mon Nov 02, 2009 3:32 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
elder73 wrote:
fredrik.eriksson wrote:
i believe the line should say:
elder73 elder73 = NOPASSWD: /sbin/shutdown


Success at last!! I edited the sudoers file to what you suggested above. Then I ran the following:

[root@localhost elder73]# numberofloops=3; for i in $(seq -w 1 $numberofloops); do scrot "$(date +%Y-%m-%d)_$i.png"; sleep 60; done; sudo shutdown -h now

You will note that I am running the above script as root which solved the problem. Previously I had been running it as a user.

Best wishes,

Bill Bradley.


Top
 Profile  
 PostPosted: Tue Nov 03, 2009 2:31 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
if you're running it as root then you won't need to use SUDO.

SUDO is just a synonym for "Super User do", meaning it executes applications as super user (a.k.a. root) and all the privileges associated with that.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Nov 03, 2009 5:13 pm   

Joined: Sun Oct 25, 2009 6:42 pm
Posts: 18
fredrik.eriksson wrote:
if you're running it as root then you won't need to use SUDO.
SUDO is just a synonym for "Super User do", meaning it executes applications as super user (a.k.a. root) and all the privileges associated with that.


Well that is interesting. I wonder why it does not work as a user. Anyway, it works as root and with that I am happy. Thank you for all your help with this project.

Best Wishes,

Bill Bradley.


Top
 Profile  
 PostPosted: Wed Nov 04, 2009 2:26 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Most likely it doesn't work because the sudoers line is not correct :P

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP