Terminal running bash/rsync script does not close with exit (MacOS High SIerra)


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 2 Weeks Ago
Quote:
Originally Posted by Don Cragun
The fact that you have to take an intermediate step to specify what is to be used to run your script tells me that something is wrong. I'm guessing that you skipped part of the 1st step I mentioned in post #5. Please show us the output from the commands:
Code:
ls -l script
head script

where script is the name of the file containing your shell script.
Here is the output,
Code:
My-iMac:rsync_backup my_user$ ls -l
total 392
-rwxrwxrwx@ 1 my_user  staff   88056 Mar  5 20:22 data_backup_icon.png
-rwxrwxrwx@ 1 my_user  staff  102078 Mar  5 20:24 external_backup_icon.png
-rwxrwxrwx@ 1 my_user  staff    1314 Mar  6 22:26 rsync_data_backup.sh
-rwxrwxrwx@ 1 my_user  staff    1794 Mar  5 23:11 rsync_external_backup.sh

Code:
My-iMac:rsync_backup my_user$ head rsync_data_backup.sh 
#!/bin/bash

I also did which just to make sure that bash was were it was expected.
Code:
My-iMac:rsync_backup my_user$ which bash
/bin/bash

How would MacOS know to read the first line and use that to know that the file is a script that should be run in bash. Is that the default behavior for any file with a .sh extension or does Finder read the first line of every file it opens to look for #!? I believe that you can set the "open with" preference in the file Get Info window as well as in finder.

As far as I can tell with what you are saying, my script file should run in Terminal by double clicking without having to do anything special. I think the problem may be that the first application I opened the file with was the Atom editor and possibly that application did something to set the OS to open .sh files in the editor. My guess is it must be something like that. It runs now as it should.

I was told that I can get the terminal window to close when the script finished by setting a preference in Terminal preferences. It would make sense that this is a MacOS Terminal configuration issue and not an issue with the script or the way the script and window are created.

The backup is running now and I will check on the preferences when the backup is finished.

LMHmedchem
# 9  
Old 2 Weeks Ago
Ah, yes. I see!

You name your shell script rsync_data_backup.sh and you have problems.

I name my shell script rsync_data_backup and it works just fine.

I don't know why, but having the extension .sh on your filename creates the problem you're having. Get rid of that extension and get rid of your problem. Smilie

Are we having fun yet?
These 2 Users Gave Thanks to Don Cragun For This Post:
Corona688 (2 Weeks Ago) LMHmedchem (2 Weeks Ago)
# 10  
Old 2 Weeks Ago
Quote:
Originally Posted by Don Cragun
I don't know why, but having the extension .sh on your filename creates the problem you're having. Get rid of that extension and get rid of your problem. Smilie
When I remove the extension, the file icon changes from the Atom text editor icon to the terminal icon. The script does work as expected now. I wonder if MacOS reads the first line of a file that doesn't have an extension to see what to open the file with.

Quote:
Originally Posted by Don Cragun
Are we having fun yet?
I'm having allot more fun now than I was with this yesterday. Smilie

On the issue of the window not closing when the script completes, if you go to Terminal > Preferences > Shell, there is a menu in the middle of the window called

when the shell exits .

There are three options on this menu,

close the window
close if the shell exited cleanly
don't close the window

mine was set to don't close the window

There are certainly cases where I would prefer to have the window stay open when the script has finished and I usually add a read statement to the end if this is the case. I'm not really sure it makes sense for that to be a global behavior since I can't see that I would want the same behavior in every script. It would be nice if exit had some options so you could specify this on a script by script basis.

There are also options in the Shell preference pane to indicate situations where ask before closing is implemented, in case that is of interest to anyone.

Thanks again,

LMHmedchem
This User Gave Thanks to LMHmedchem For This Post:
Don Cragun (2 Weeks Ago)
# 11  
Old 2 Weeks Ago
My apologies for not investigating this the other day, I forgot all about it.

exit does have an option for exactly the purpose of informing the thing which called it, it returns a number 0 for success and 1-127 for various errors. I suspect that's what "close if the shell exited cleanly" is about. exit 0 to close cleanly and exit 1 to make the terminal wait and display what happened.

Drawback is, if these scripts are called by anything else, nonzero means error, not just close/not close. crontab sends emails when programs exit nonzero, for example.

Perhaps your scripts could take a --terminal commandline switch which tells them to exit nonzero?
This User Gave Thanks to Corona688 For This Post:
LMHmedchem (2 Weeks Ago)
# 12  
Old 2 Weeks Ago
Quote:
The script seems to work, but when I key enter, instead of the terminal window closing like it does in windows under cygwin, I get the following message,
Code:
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

The terminal does not seem to function anymore but it does not close and I have to manually close it. It's not the end of the world but I don't understand why it doesn't close down on the exit command. Can anyone explain how to fix this?

It is a macOS thing. You must right click on the Terminal icon on the Dock, and select Quit.

See the screenshot attached.
Terminal running bash/rsync script does not close with exit (MacOS High SIerra)-png
This User Gave Thanks to johnprogrammer For This Post:
LMHmedchem (2 Weeks Ago)
# 13  
Old 2 Weeks Ago
There is an option in the MacOS terminal to close the window when the shell exits. That is what I use.

Image
These 2 Users Gave Thanks to Neo For This Post:
johnprogrammer (2 Weeks Ago) LMHmedchem (2 Weeks Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
If you run macOS High Sierra version 10.13.1, be sure to install today's update. Don Cragun OS X (Apple) 6 12-01-2017 01:05 PM
Capture power button press on MacOs High Sierra? xedge UNIX for Beginners Questions & Answers 0 10-12-2017 05:04 PM
[BASH] Script to manage background scripts (running, finished, exit code) sea Shell Programming and Scripting 17 10-18-2014 04:15 PM
Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop? hilltop_yodeler Shell Programming and Scripting 9 03-06-2014 05:03 PM
[Solved] Rsh does not close connection on exit rbatte1 Shell Programming and Scripting 1 02-17-2014 07:53 AM
Close UNIX terminal bobbygsk UNIX for Advanced & Expert Users 11 10-04-2013 02:56 PM
How to exit from the parent script while the child is running? Little Shell Programming and Scripting 3 10-04-2013 11:33 AM
close a mac terminal cokedude UNIX for Advanced & Expert Users 1 12-21-2011 07:56 AM
SSH from MacOS X or PPC Debian to SUSE # Odd terminal chars # Eventual scripting pagrus UNIX for Dummies Questions & Answers 14 12-08-2011 12:01 AM
script does not close terminal after running Ditzyken Shell Programming and Scripting 3 09-01-2011 01:34 PM
Capturing the exit status of the script running in background paragkalra Shell Programming and Scripting 2 04-10-2010 04:22 PM
User cant log in (close terminal) lo-lp-kl AIX 2 02-23-2010 11:32 AM
Bash script to back up with rsync ajmoreti Shell Programming and Scripting 3 11-16-2009 05:36 AM
running terminal with script cdfd123 Shell Programming and Scripting 3 12-13-2008 02:35 AM
Running a script without a terminal session jjinno Shell Programming and Scripting 3 10-03-2007 01:50 AM