1

I have shell scripts like

#!/bin/bash
while true;do
   #Code HERE!
   #Pushing Data to DB
   echo "Data to DB"> /root/schip.log 2>&1
done

This is script is continuously running and gathering info on server and then sending data to DB(TimeStamp DB). I don't why, sometimes the scripts are dieing. In logs I can't see any thing. In same way, I saw in Python script. Python script like this

import <stuff>
while True:
   #Code HERE
   #Push data to DB
   print "Data to DB"

So, what could be the reasons?, how do I prevent? and how can I enable the logs(In python and Shell) to know the reason?. Thanks!

  • do any of the scripts have set -e in them? if so, then any error or any non-zero exit code from any command (e.g. while shoveling data into the db) would cause the script to exit. – cas Oct 27 '15 at 7:27
  • No, there no set -e in scripts. In Python script, I'm using potsdb module to push data to Time stamp DB like this metrics = potsdb.Client(<serverIP><serverPort>) then metrics.send(<metrics_name>, <Tag1=value1>). So, there could be problem while pushing to DB? – Veerendra Oct 27 '15 at 7:33
  • So, what was it in the end? – Stéphane Chazelas Oct 27 '15 at 12:53
4

A few things that may cause a shell to exit (not exhaustive):

  • calling the exit utility. Let's not forget about the obvious
  • calling the return utility. In the case of bash that will return only if in a function or sourced file.
  • exec cmd. That will execute cmd in the same process so in effect breaking out of that loop. The script will end when cmd exits.
  • set -e/set -o errexit is enabled (see also the SHELLOPTS environment variable for bash) and a command exits with an error.
  • set -u/set -o nounset is enabled and an unset variable is referenced.
  • a DEBUG or ERR trap is defined that calls exit.
  • Failing special builtins. Failure of special builtins (like set, :, eval...) causes the shell to exit. In the case of bash though, that only happens in POSIX mode (like when POSIXLY_CORRECT is in the environment or when invoked as sh...) and even then not for all special builtins. For instance : > / will cause the shell to exit.
  • as mentioned by @schily, syntax error (like in code that is only reached conditionally).
  • division by 0 (in $((1/x)) or ${array[1/x]}).
  • internal bash error for instance because some limit is reached:
    • fails to allocate memory
    • fails to fork a process
    • stack size exceeded (for instance when using function recursion)
    • Some other limits in place via ulimit (which may also cause some signals to be sent).
  • killed by a another process. Another process can call kill() to explicitly kill the interpreter of your script.
  • killed by the system.
    • SIGINT/SIGQUIT. If you press ^C/^\.
    • SIGHUP. If the terminal is disconnected.
    • SIGSEGV/SIGBUS/SIGILL. The bash command does something wrong (a bug) or failing hardware (memory).
    • SIGPIPE: builtin (echo, printf) writing to a now-closed pipe or socket (could also happen for error messages if stderr is a pipe).

The first thing to check would be the error messages and the exit status.

0

A syntax error in a script will terminate the script.

If your script code includes shell variables that may include spaces in some cases, this may cause a syntax error in case the shell variable is not enclosed in double quotes.

  • Script is not terminating because of syntax error, I checked. I removed while statements and then I executed->Works Fine!. Actually, I ran those script with nohup like nohup sh script.sh &, that will create the nohup.out. I can tail -f nohup.out to view the data weather, it is running or not->This is also works fine(But suddenly it dieing! somtime) – Veerendra Oct 27 '15 at 12:47
  • 1
    You are mistaken: even with bash, scripts terminate from a syntax error. You may try to set -x to get the location in the script that aborts. – schily Oct 27 '15 at 12:55
0

Possibly your Shell is sourcing some external code with the "." command.
That code is interpreted by the same shell instance. If that code has a syntax error or that code performs an "exit", the calling script halts. It is possible that this script is not called at each loop so your job is running a while before it fails.

example with a syntax error that will fails after 3 loops

#!/bin/sh
i=3
while true
do
let i=i-1
[ $i -eq 0 ] && . ./a 2>/dev/null

sleep 2
done

"a" script is missing "fi"

if  true
then 
   echo a 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.