pricecharts

track prices of consumer electronics
Log | Files | Refs | README

commit 47b5037c547712d370a6134c7e204a4bed9b8414
parent e77f256e57de4177b8c0b6c6395d05068cce725d
Author: kyle <kyle@getaddrinfo.net>
Date:   Tue, 10 Nov 2015 22:11:24 -0700

ps_scrape: sleep ~10 min when run from cron

- do this to tidy up the ps output on the scraping machine
- before this was a shell hack ($RANDOM), this is cleaner
- also simplifies install/setup dependencies

Diffstat:
Mps_scrape | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/ps_scrape b/ps_scrape @@ -2,6 +2,7 @@ use strict; use warnings; +use BSD::arc4random qw(arc4random_uniform); use Config::Grammar; use Email::Simple; use Email::Send; @@ -21,6 +22,7 @@ getopts("nptv", \%args); $| = 1 if ($args{v}); +sleep_if_cron(); my $cfg = get_config(); my $ua = new_ua($cfg->{general}, $args{v}); my $dbh = get_dbh($cfg->{general}{db_dir}, $args{v}); @@ -488,3 +490,26 @@ sub sleep_rand printf "$header: (%ss wait)\n", $sleep if ($args{v}); sleep $sleep unless ($args{t}); } + +sub can_open_tty +{ + no autodie; + return open(my $tty, '+<', '/dev/tty'); +} + +sub sleep_if_cron +{ + if (can_open_tty()) { + return; + } + + # 577s is appx 9.5 min + my $sleep = arc4random_uniform(577); + print "info: script run from cron, sleeping $sleep s\n" if ($args{v}); + + # modify ps output to show what state the program is in + my $old_argv0 = $0; + $0 .= " (sleeping)"; + sleep $sleep; + $0 = $old_argv0; +}