Linux – How to not run /etc/profile.d/custom.sh during Secure FTP login

bashlinuxscriptingsftpstartup

When I attempt to Secure FTP (using WinSCP) to my CentOS 6.4 box I'm prompted with a message that states:

Received too large (168438314 B) SFTP packet. Max supported packet size is 1024000 B.

This error is typically caused by message printed from startup script (like profile). The message may start with ""\n\n**"" 

Cannot initialize SFTP protocol. Is the host running an SFTP server

This appears to be caused by my /etc/profile.d/custom.sh script which gets run during login. Its setup to output a welcome message, as well as a system summary (CPU/Memory/Disk Usage).

I really need the custom.sh script to run when logging into the local console OR when logging in via SSH. However I don't want it to run when logging into Secure FTP (SFTP).

Is there some way to configure it not to run when logging in via Secure FTP?

Or alternatively if I can't stop it from running is there a way I can detect that its being run during a secure FTP login and not output anything. So in pseudo code something like:

If logged in via secure FTP then output nothing
Else if logged in via SSH or Local Console output message of the day (system stats).

Thanks!
Brad

UPDATE – There is documentation supporting this issue here: http://winscp.net/eng/docs/requirements#remote_environment

The suggestion is to detect if the session is interactive or not. I tried doing that by adding the following to the top of my custom.sh but its still not working:

if [ -z $PS1 ]; then
### if [ -v PS1 ]   # On Bash 4.2+ ...
    # non-interactive
    return
fi

Then the rest of my script runs outputting the MOTD.

Should SecureFTP be detected as non-interactive or interactive? If its non-interactive why isn't the code above existing the script when I attempt to login via Secure FTP?

Best Answer

Maybe standard method of checking the fd ’0′ ?

if [ -t 0 ]; then
# do stuff
fi