0

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!

keynesiancross
  • 3,441
  • 15
  • 47
  • 87
  • Can you reference the other answer here? – tenacity Jul 11 '22 at 14:13
  • I think runProgram.sh is with CRLF line terminators. – tenacity Jul 11 '22 at 14:15
  • added the link. I'm not sure what you mean by CRLF line terminators? I'm totally new at this. – keynesiancross Jul 11 '22 at 14:15
  • Also - why not have the while loop inside the python script? & call the python script directly? – tenacity Jul 11 '22 at 14:16
  • Also why are you doing `chmod -x run Program.sh` Instead you must be giving it execute permissions by doing `chmod +x runProgram.sh` – tenacity Jul 11 '22 at 14:20
  • Yeah, I seriously have no clue what I’m doing. If you could lay it out for me like you would a 5yr old that would be great. – keynesiancross Jul 11 '22 at 14:23
  • Couple things. **1** remove the space between `#!` and `/usr/bin/env`. **2** you could put the loop and sleep directly in the python script (like @HariharanRagothaman mentionned). **3** CRLF: when a file is created using a Windows editor (ex notepad) the files end with CR (carriage return) and LF (line feed). These can cause issues when you run the files on Linux. See https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-lf for ways to remove them. – Nic3500 Jul 11 '22 at 14:24

2 Answers2

1

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.

tenacity
  • 456
  • 1
  • 5
  • 14
0

This command makes the file non executable:

chmod -x runProgram.sh

What you want to do is to make executable like so:

chmod +x runProgram.sh

Turn both, your shell script and python script executable.

then you can run it by typing ./runProgram.sh or bash runProgram.sh.

Macko
  • 360
  • 2
  • 11