0

I am trying to run PowerShell script from cron (Debian/Ubuntu based Bitnami distro).

At the moment I'm trying to run it using a .sh script, which calls the .ps1 script and logs an execution time like so:

#!/bin/bash
dt=$(date +"%d.%m.%Y %H:%M:%S")
echo $dt executing >> run.log
/usr/bin/pwsh -File "/home/bitnami/tale_2.ps1"

When I run this script directly from console, everything works. However, when I try to run it from cron (user and root) using:

* * * * * cd /home/bitnami && /home/bitnami/run.sh

I've tried also to run it directly:

* * * * * pwsh -File "/home/bitnami/tale_2.ps1"

even with pwsh with absolute path with no luck at all to run the ps1 script file. However, when I run the .sh file directly from console, it executes the ps1 file. run.log gets updated either through cron, also running directly.

This is the script, I'm trying to excecute with cron.

#!/opt/microsoft/powershell/7/pwsh -Command


# [System.IO.Ports.SerialPort]::getportnames()

$port = New-Object System.IO.Ports.SerialPort
$port.PortName = “/dev/ttyr00”
$port.BaudRate = “9600”
$port.Parity = “None”
$port.DataBits = 8
$port.StopBits = 1
$port.ReadTimeout = 5000 # 5 seconds
$port.DtrEnable = “true”
$port.open() #opens serial connection
$port.IsOpen


$dataSQL = [System.Collections.ArrayList]@()
$count = 500
for($i=0; $i -lt $count; $i++){
$dataSQL += ""
}

$comNO = 0

function convertToHex($bytes, $offset, $count)
{
    $result = '';
    for ($i = $offset; $i -lt ($offset + $count); $i++) {
        $result += ' ' + $bytes[$i].toString("X2");
    }
    return $result;
}

function convertToInt($bytes, $offset, $count)
{
    $result = @()
    for ($i = $offset; $i -lt ($offset + $count-4); $i+=2) {
        $pom_result = ($bytes[$i+1]*256 + $bytes[$i])  ;
        If ($pom_result -gt 32767 ) {$pom_result -= 65535 }
        $result += $pom_result  ;
    }
    return $result;
}





$s=New-Object System.Collections.Generic.List[string]
$s.Add("AA 55 00 00 01 00 08 00 00 00 08")
$s.Add("AA 55 00 00 01 00 1C 00 00 00 1C")
$s.Add("AA 55 00 00 01 00 30 00 00 00 30")
$s.Add("AA 55 00 00 01 00 44 00 00 00 44")
$s.Add("AA 55 00 00 01 00 58 00 00 00 58")
$s.Add("AA 55 00 00 01 00 6C 00 00 00 6C")
$s.Add("AA 55 00 00 01 00 34 01 00 00 35")






$s.ForEach({
    Start-Sleep -m 1000 # wait 100ms seconds until device is ready
    write-host $args[0].ToString()




        try
        {

        # [Byte[]] $request = 0xAA,0x55,0x00,0x00,0x01,0x00,0x44,0x00,0x00,0x00,0x44
        # [Byte[]] $request = 170,85,0,0,1,0,68,0,0,0,68

        [byte[]] $request = $args[0] -split ' ' | foreach-object { invoke-expression "0x$_" }
        write-host -noNewline 'send : ';
        write-host $request; #  Display parameters


        $port.Write($request, 0, $request.Count)
        Start-Sleep -m 500

            #  receive
            $recvBuffer = new-object byte[] 128;
            $recvLen    = $port.read($recvBuffer, 0, 128)-1;

            #  display
            write-host -noNewLine 'recvLen: ';
            write-host $recvLen;

             # write-host -noNewLine 'recvBuffer: ';
             # write-host $recvBuffer;
             # write-host ' ';

            #write-host -noNewLine 'receive: ';
            #convertToHex $recvBuffer 0 $recvLen | write-host;

            #write-host -noNewLine 'receive Int: ';
            #convertToInt $recvBuffer 2 $recvLen | write-host;
            $data_int = convertToInt $recvBuffer 2 $recvLen
            write-host -noNewLine 'data_int: ';
            write-host $data_int;


            $smernik = $data_int[2]
            write-host -noNewLine 'smernik: ';
            write-host $smernik;

            #$dataSQL.Add($data_int[3]);
            #$dataSQL[$smernik] = ($data_int[3]);
            for ($i = 0; $i -lt 10; $i++) {
                $dataSQL[$smernik + $i] = ($data_int[$i+3]);
                $comNO++;
            }


            write-host ' ';


        # while($myinput = $port.ReadExisting())
        # {
        # $myinput

        # }
        }


        catch [TimeoutException]
        {
        # Error handling code here
        }

        finally
        {
        # Any cleanup code goes here
        }


    })
write-host -noNewLine 'dataSQL: ';
write-host $dataSQL



#pause
$port.Close() #closes serial connection
$port.IsOpen;

Start-Sleep -Seconds 1



#./tale-sql.sh "INSERT INTO public.datale(an_01, an_02, an_03, an_04) VALUES ($var1,$var2,$var3,$var4)"
#./tale-sql.sh "INSERT INTO public.datale(an_01, an_02, an_03, an_04) VALUES ($dataSQL)"
#./tale-insert_sql.sh "$dataSQL"


$var_an01 = $dataSQL[8]
$var_an02 = $dataSQL[9]
$var_an03 = $dataSQL[10]
$var_an04 = $dataSQL[11]
$var_an05 = $dataSQL[12]
$var_an06 = $dataSQL[13]
$var_an07 = $dataSQL[14]
$var_an08 = $dataSQL[15]
$var_an09 = $dataSQL[16]
$var_an10 = $dataSQL[17]
$var_an11 = $dataSQL[28]
$var_an12 = $dataSQL[29]
$var_an13 = $dataSQL[30]
$var_an14 = $dataSQL[31]
$var_an15 = $dataSQL[32]
$var_an16 = $dataSQL[33]

/home/bitnami/tale-sql.sh "INSERT INTO public.datale(an_01, an_02, an_03, an_04, an_05, an_06, an_07, an_08, an_09, an_10, an_11, an_12, an_13, an_14, an_15, an_16) VALUES ($var_an01,$var_an02,$var_an03,$var_an04,$var_an05,$var_an06,$var_an07,$var_an08,$var_an09,$var_an10,$var_an11,$var_an12,$var_an13,$var_an14,$var_an15,$var_an16)"

RoSi
  • 37
  • 1
  • 8
  • What does "no luck at all" mean? Does `run.log` get updated? What does the PowerShell script do, does it write any logs or have any side effects you can observe? If so, what _did_ you observe? – Mathias R. Jessen Mar 23 '22 at 10:08
  • Yes, run log get updated from running directly from console and also from cron. However, ps1 script is executed only from console run, not from cron. Sorry, I didn't stated that. PowerShell script writes data to a database, which is done running through console (.sh) but not from cron. – RoSi Mar 23 '22 at 10:21
  • Please review [Cron job not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) for general `cron` troubleshooting tips, then [edit] your question with more diagnostics. As it stands, this doesn't have enough details to conclude anything. Does the `pwsh` script contain commands which need a particular `PATH`? – tripleee Mar 23 '22 at 10:41
  • Tangentially, the `cd` appears useless. – tripleee Mar 23 '22 at 10:41
  • @tripleee : ```cron``` is running, i know that because I got entry logged in ```run.log``` file. PowerShell script does not need a particular path, therefore I agree, that cd is useless. When i run ```.sh``` file from console, the ```.ps1``` script gets executed. When from ```cron```, then not. It only writes to ```run.log``` file. Thanks for the edit btw. – RoSi Mar 23 '22 at 10:49
  • I'll try again: please show us the contents of the script, perhaps reduced to a [mre]. The question I linked to contains a number of tips for troubleshooting `cron` jobs generally; it's not about "cron not running" really. – tripleee Mar 23 '22 at 10:54
  • Right, I've edited my question to include the .ps1 script I'm trying to run from cron. – RoSi Mar 23 '22 at 11:03
  • And what do you see in the system logs from `cron`? Do you receive email with an error message? – tripleee Mar 23 '22 at 12:54

0 Answers0