@GrandFather I would rather not combine my scripts, as they each have very different purposes, and the two scripts are also used independently on other machines. I think that creating a daemon is worth a try. As I have no formal IT training, this is going to be an interesting learning experience.
As to the common module with a lock file, I am still stuck with how to have two scripts accessing a common module. I have now tried running a script that has package functions and calls the two test scripts. Pretty messy to put it mildly, but is was just an experiment.
#!/usr/bin/perl
package Hvac::i2cAccess;
use strict;
use HiPi::Device::I2C;
use Exporter qw( import );
our @EXPORT_OK = qw( i2c_test1 i2c_test2 );
my $copyit = "Empty";
# call test scripts
# test1 just gets the $copyit value
# test2 sends an i2c object and gets the voltage value in return
# the voltage value is also copied into $copyit
do "/home/huw/cron_scripts/test1.pl";
print "A\n";
do "/home/huw/cron_scripts/test2.pl";
print "B\n";
do "/home/huw/cron_scripts/test1.pl";
print "C\n";
while( 1 ){
sleep 1;
}
exit 0;
# ********************************************************* #
sub i2c_test2 {
# i2c object passed from test2.pl
my $obj = $_[0];
# read 2 bytes of data from bus voltage register (address 0x02)
my @bvr;
eval{
@bvr = $obj->bus_read( 0x02, 2 );
1;
}
or do {
$bvr[0] = 0;
$bvr[1] = 0;
};
# result in upper 13 bits - big-endian order
my $busV = pack 'C2', $bvr[0], $bvr[1];
$busV = ( unpack 'S>', $busV ) >> 3;
# scale bus voltage register value
my $bv = 16;
my $bvScale = 4000;
$busV = $bv / $bvScale * $busV;
$copyit = $busV;
return $copyit;
}
# ********************************************************* #
sub i2c_test1 {
print "Test 1 subroutine\n";
return "Copy $copyit";
}
# ********************************************************* #
# must end modules with a 'true' value
1;
but I don't get past the first 'do'. I presume that 'do' is waiting for the script it calls to end and return a value. 'A' is never printed The module code without the 'calls' to the two test scripts was used to test the common module approach, but as I said, it looks like each test script invokes a separate copy of the module. |