help on getting a script to run

Status
Not open for further replies.

jimmyt

Dabbler
Joined
Oct 9, 2014
Messages
18
I have completly confused myself on trying to get a script to run and hope someone can set me on the path to enlightenment :)

I have seren's rrd temp graphing script up and running just fine. I am trying to create a script that emails me the graph png file early in the am so I can take a quick look as part of my morning emails to make sure things are ok. If I paste the commands into the shell it runs just fine.

Here is the script:

Code:
#!/usr/local/bin/bash
email="x@x.com"
subject="CPU Temps"

	uuencode /mnt/Camera/script/temps-2min-cpus.png temps-2min-cpus.png > /tmp/out.mail
	mail -s "${subject}" "${email}" < /tmp/out.mail



since it runs in the shell I took it and created the file "mailtemps2.sh" via notepad ++

The file is located in /mnt/Camera/script

If I try and run the script from the shell with the following command "[root@freenas ~]# ./mnt/Camera/script/mailtemp2.sh"
I get this: "bash: ./mnt/Camera/script/mailtemp2.sh: No such file or directory"

If I change into the directory and try to run it "[root@freenas /mnt/Camera/script]# .mailtemp2.sh"
I get this: "bash: .mailtemp2.sh: command not found"

Finally if I try to set it as a cron job I get an email with the subject: "Cron <root@freenas> PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin" /mnt/Camera/script/mailtemp2.sh > /dev/null"

and the text of the email is "/bin/sh: /mnt/Camera/script/mailtemp2.sh: not found"

I am a complete novice when it comes to scripting but once I see how it works I can usually figure it out.. not this time.. I am stumped. I am sure its something simple, but to me its confusing as hall.. :) any help would be greatly appreciated
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
First you didn't specify what version of FreeNAS you were using but that hopefully will not make a difference, it shouldn't.

I looked at your code and even tested it on my machine and didn't find any issues with it.

Now getting to the execution part... Did you change the file mailtemp2.sh to be executable using chmod +x mailtemp2.sh ? If not then you must call the script using sh preceeding the script name. Example: sh /mnt/Camera/script/mailtemp2.sh

Also when running the command in the subdirectory where it resides you enter ./mailtemp2.sh ensuring you have that slash after the decimal point, assuming you have made it executable of course, which I do recommend.
If I try and run the script from the shell with the following command "[root@freenas ~]# ./mnt/Camera/script/mailtemp2.sh"
I get this: "bash: ./mnt/Camera/script/mailtemp2.sh: No such file or directory"
That is odd becasue if you did not make the file executable then I get a Permissions Denied response. If it is executable then it works fine for me, I don't get the No such file or directory answer. This would make me assume you had the path wrong or the filename is wrong.

So the name of your pool is "Camera" ? Just asking for confirmation.

While you can use notepad to create a script, I'd advise to use a text editor like ee within the shell. This will ensure you do not have any unseen characters in the text, this is my personal preference.
 

zoomzoom

Guru
Joined
Sep 6, 2015
Messages
677
While you can use notepad to create a script, I'd advise to use a text editor like ee within the shell.
If wanting the ease of using a notepad editor with syntax highlighting and other features, I always recommend Atom, which is available on both Windows and Linux.
  • A general FYI:
    • Anyone using Windows should be using an editor designed specifically for coding, else one will end up with broken files due to EOL CRLF [Windows] versus EOL LF [BSD/Linux]
      • Atom is fully customizable with thousands of official and user generated plugins and themes, with the default theme being a dark gray theme, which I prefer over an editor with a white/bright background.

 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
6,421
If wanting the ease of using a notepad editor with syntax highlighting and other features, I always recommend Atom, which is available on both Windows and Linux.
  • A general FYI:
    • Anyone using Windows should be using an editor designed specifically for coding, else one will end up with broken files due to EOL CRLF [Windows] versus EOL LF [BSD/Linux]
      • Atom is fully customizable with thousands of official and user generated plugins and themes, with the default theme being a dark gray theme, which I prefer over an editor with a white/bright background.
Notepad++ does all this and should work just fine. Just make sure there are correct line endings.
 

zoomzoom

Guru
Joined
Sep 6, 2015
Messages
677
Notepad++ does all this and should work just fine. Just make sure there are correct line endings.
It provides similar functionality, however it's customizability is ~1% of that of Atom... or at least the customizable content available for Notepad++ is <1% of Atom, not to mention many of the plugins are insecure to use, further hampering the customizability of Notepad++
  • Additionally, most prefer a dark background for coding, of which Notepad++ cannot be configured to reliably do without inconveniencing the user.
    • For example, if you've customized the Windows GUI with custom colorizations, and you configure Notepad++ with a dark background (selecting it to be globally applied, as Notepad++ seems to believe many syntaxes need to have colored text on top of a colored background), upon opening a new file, it will always default to black text until one either opens a new file tab or selects a language from the language list.
      • I've spent days and days trying to get around this, and it appears there isn't a way as Notepad++ pulls many of it's colors from the OS itself, versus Atom, which doesn't rely on the OS for colors at all, hence the cohesive experience.

  • The one thing Notepad++ does have that Atom doesn't, or at least I haven't found a plugin that does so on Atom, is NPPAdmin, which if saving a file that requires Admin rights to do so, NPPAdmin will relaunch the save command as Admin.
Since Atom is also available on Linux, all one has to do is copy the .atom folder to another OS in order for settings & plugins to be transferred, allowing for a cohesive experience between Windows <-> Linux extremely easily.
 
Last edited:

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
I get this: "bash: ./mnt/Camera/script/mailtemp2.sh: No such file or directory"
That's because . stands for the current directory. At the time you tried to run that, you were in the root user's home directory, which is /root/. So you were trying to execute /root/mnt/Camera/script/mailtemp2.sh, which doesn't exist.
I get this: "bash: .mailtemp2.sh: command not found"
That's because you'd need a slash separating . and the filename: ./mailtemp2.sh.
 

jimmyt

Dabbler
Joined
Oct 9, 2014
Messages
18
I appreciate everyone's help so far! I am using 11.0-U3 and the name of the volume is "Camera" (it is a mirrored set - 2 @ 3tb drives) and as you can guess - it is storage for ip cameras. I also run the plex jail on it and decided to create the script directory in there for the temp monitoring as well.

as per danb suggestion - if I run it in the shell from either the /mnt/Camera/script directory with
Code:
./mailtemp2.sh
or from the root directory with
Code:
/mnt/Camera/script/mailtemp2.sh

I get the following error
Code:
[root@freenas ~]# /mnt/Camera/script/mailtemp2.sh							  
bash: /mnt/Camera/script/mailtemp2.sh: /bin/sh^M: bad interpreter: No such file
or directory

I also changed the first line to
Code:
#!/usr/local/bin/bash
and I get a similar error. If I am understanding this, the lines
Code:
#!/usr/local/bin/bash
and
Code:
#!/bin/sh
is where it cant find what it needs?
I opened the file in ee and all of the lines have a ^M after them except for the last line which has nothing..
view from ee command in shell
Code:
[ (escape) menu ^y search prompt ^k delete line   ^p prev li	 ^g prev page
^o ascii code	^x search		^l undelete line ^n next li	 ^v next page
^u end of file   ^a begin of line ^w delete word   ^b back 1 char ^z next word
^t top of text   ^e end of line   ^r restore word  ^f forward char			
^c command	   ^d delete char   ^j undelete char			  ESC-Enter: exit
=====line 6 col 51 lines from top 6 ===========================================
#!/bin/sh^M																	
email="X@X"^M													
subject="CPU Temps"^M														  
^M																			
	uuencode /mnt/Camera/script/temps-2min-cpus.png temps-2min-cpus.png > /tmp/o
	mail -s "${subject}" "${email}" < /tmp/out.mail  


another note.. I try to run chmod +x on the file and I get operation not permitted
Code:
[root@freenas /mnt/Camera/script]# chmod +X mailtemp2.sh						
chmod: mailtemp2.sh: Operation not permitted 

If I go into storage and try to change the permissions under unix permission types everything is checked except "write" under "other". If I change that, it says permissions changed, however, when I go back to it, the "write" under "other" is unchecked again. All other volumes have it checked.

thanks again and any advice is greatly appreciated!
 
Last edited:

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
bash: /mnt/Camera/script/mailtemp2.sh: /bin/sh^M: bad interpreter: No such file
That's an end-of-line problem. You wrote the script under Windows and saved it with Windows/DOS end-of-line characters (CR/LF). You need Unix EOL characters (LF only). You can probably fix this in Notepad++. Although you surely have /bin/sh, you don't have /bin/sh^M, and that's why it's giving you an error.
[root@freenas /mnt/Camera/script]# chmod +X mailtemp2.sh chmod: mailtemp2.sh: Operation not permitted
That means you're using Windows permissions. I'd suggest creating a separate dataset for your scripts and using Unix permissions on it, though there are probably other ways to accomplish this.
 

jimmyt

Dabbler
Joined
Oct 9, 2014
Messages
18
thanks! turns out that was it.. I set notepad ++ to windows.. didnt realize that you had to change it to unix.. so now I can run the scripts from the shell and they work.. next step is to get a cron job running! Although I still cant set the "write" permission on "other". Even deleted the windows share so there is now no shares for that dataset. I'll report back on the cron job after I get it going
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Glad things worked out for you.
 
Status
Not open for further replies.
Top