AWS Linux: ensure that flaky script keeps running

Issue

I am trying to write a nanny script, to use in AWS’s Linux terminal, to run a python script that can be a bit flakey at times. I am very new to using the terminal / bash, etc, so I could be missing something totally obvious / second nature to others. Here is what I have:

#! /usr/bin/env bash

while true:
    script.py
    sleep 1  # pause, so if script.py immediately dies we don't burn a core

I put the above code in a runProgram.sh file, located in the same directory as my python program. I then went into the terminal, and ran:

chmod -x runProgram.sh

followed by:

bash runProgram.sh

The terminal throws the following error:

"line 6: syntax error: unexpected end of file".

I took the bash code right from a stackoverflow response though, so I’m not sure what I’m missing in terms of syntax etc. any help is appreciated!

Solution

I was able to reproduce your issue, by directly copy-pasting the snippet you pasted.

Modified your script slightly. Feel free to copy-paste directly.

#!/bin/bash
while [ True ]
do
  python3 script.py
done

To know more about CRLF

Also this is my current bash version

/tmp:$ bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.

Why does chmod +x do?

It essentially gives execute level permissions for your script of interest.

Answered By – Hariharan Ragothaman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published