Search code examples
powershellabaqus

ABAQUS Powershell Automation: Running a job for 1 hour then terminate and proceed to analyse the next job


Newbie to Powershell here. Sorry if someone has ask this in the past but I could find an example that relate to Abaqus. So what I am planning to do is to run a few jobs, one after the other sequentially via Powershell automation, my problem is Start-Process runs Abaqus synchronously which it wont proceed to the next line of code which will make the script sleep for 1 hour before killing it until the job is fully completed. I then learned I can run Abaqus asynchronously with Start-Job however I have no luck getting anything with my script below. Am I doing something wrong here? Seems like my scriptblock is wrong.

Thanks very much!

#
# Script.ps1
#
# Choose a job file containing all the jobs
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Filter           = 'Text File (*.txt)|*.txt|Batch File (*.bat)|*.bat'
}
$Null = $FileBrowser.ShowDialog()

# Assign job files directory to path
$myPath = Split-Path -Parent $FileBrowser.FileName
$jobsFile = Split-Path -Leaf $FileBrowser.FileName
Set-Location -Path $myPath -PassThru
Write-Host "Selected jobs file is <$jobsFile> located in $myPath" | Out-Host
#echo "My selected path is $($myPath)"
#Write-Host "My selected path is $($myPath)"
$jobsList = Get-Content -Path $myPath'\'$jobsFile
$jobCPUs = '-cpus ' + 4
foreach($job in $jobsList)
{
    #Write-Host "Job = $job"
    $jonINP = $job + ".inp"
    $jobCommand = 'job=' + $job + ' -input ' + $job + ' ' + $jobCPUs #+ ' interactive'
    Write-Host "Job Command is < $jobCommand >"
    #$myProcess = Start-Process Abaqus -NoNewWindow -Wait -ArgumentList $jobCommand -PassThru
    #Start-Sleep -Seconds 10
    #Stop-Process Abaqus
    #$p = Get-Process -Name $myProcess
    #Stop-Process -InputObject $p
    
    $scriptBlock = {
        Start-Process Abaqus -ArgumentList $jobCommand
    }
    $myJob = Start-Job -ScriptBlock $scriptBlock
    #Start-Job Abaqus -NoNewWindow -ArgumentList $jobCommand -PassThru
    Start-Sleep -Seconds 15
    $myJob | Remove-Job
    
}

No success with Start-Process as job was ran synchronously. Tried, Start-Job to execute as background job but no luck getting it to run.


Solution

  • You won't get any output from a process started using Start-Process so that option should be discarded. You're also missing $using: scope modifier to reference a variable outside the job scope.

    If you want to start an asynchronous job per item in $jobList, then wait some tome for them and the get their output, the code would be:

    # start a job per item in `$jobList`
    $jobs = foreach ($job in $jobsList) {
        $jonINP = $job + '.inp' # <= This variable is assigned but never used
        $jobCommand = 'job={0} -input {0} {1}' -f $job, $jobCPUs
        Start-Job -ScriptBlock { Abaqus $using:jobCommand }
    }
    
    # wait 15 seconds for all jobs synchronously
    $jobs | Wait-Job -Timeout 15 
    # then stop them and get their output
    $jobs | Stop-Job -PassThru | Receive-Job -Wait -AutoRemoveJob