LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cannot execute binary file (https://www.linuxquestions.org/questions/linux-newbie-8/cannot-execute-binary-file-4175539212/)

aavika 04-09-2015 08:26 AM

cannot execute binary file
 
hello everyone,
I created a script file (info.sh) in linux centos 6.
location of file is '/etc/init.d/info.sh'
content of file is:

#!/bin/sh
#To get the MAC address
ifconfig | grep HWaddr
#To get the HDD serial no.
hdparm -I /dev/sd? | grep 'Serial\ Number'
#To get the HDD size
hdparm -I /dev/sda |grep "device size"

gave the permission by: chmod 777 /etc/init.d/info.sh
but when i run this file by: /etc/init.d/info.sh
it gives an error like...
-bash: /etc/init.d/info: cannot execute binary file

what should i do?? Actually i have to run this file during boot up..

thanks in advance..:)

schneidz 04-09-2015 08:35 AM

Quote:

Originally Posted by aavika (Post 5344885)
hello everyone,
I created a script file (info.sh) in linux centos 6.
location of file is '/etc/init.d/info.sh'
content of file is:

#!/bin/sh
#To get the MAC address
ifconfig | grep HWaddr
#To get the HDD serial no.
hdparm -I /dev/sd? | grep 'Serial\ Number'
#To get the HDD size
hdparm -I /dev/sda |grep "device size"

gave the permission by: chmod 777 /etc/init.d/info.sh
but when i run this file by: /etc/init.d/info.sh
it gives an error like...
-bash: /etc/init.d/info: cannot execute binary file

what should i do?? Actually i have to run this file during boot up..

thanks in advance..:)

seems like theres a typo somewhere ?

jpollard 04-09-2015 09:12 AM

First, it should not be mode 777. At most it could be 755. Having world write access would allow anyone to change the file... and be able to gain root access when it is run by root - at boot time or any other time.

Next, for it to be run at boot time, you might check the security labels of other scripts... If the script doesn't have the proper label it can't be executed.

this changes in CenOS 7, I believe the security label changes to help isolate boot utilties from introducing security failures (which would be caused by having world write access).

aavika 04-09-2015 12:05 PM

Okay 755 chmod
Boot up is 2nd or final stage
Firstly I have to run manually..
And its givin me same error even manually
Is there any code error? Or syntax and all...

---------- Post added 04-09-15 at 12:05 PM ----------

Okay 755 chmod
Boot up is 2nd or final stage
Firstly I have to run manually..
And its givin me same error even manually
Is there any code error? Or syntax and all...

aavika 04-09-2015 12:06 PM

Okay 755 chmod
Boot up is 2nd or final stage
Firstly I have to run manually..
And its givin me same error even manually
Is there any code error? Or syntax and all...

zeebra 04-09-2015 12:35 PM

I am not sure what the problem is here exactly, and not figuring out exactly what you have done.

Did you do chmod +x ?
Are you the owner of the file? Or better yet, depending on the distro, is the "user" allowed to run "hdparm" and the script?

What are you trying to accomplish exactly? With the script...

Do you run it at boot or want to run it manually by typing in "sh script.sh" or "./script.sh"..


As you probably see, I dont work much with scripts, but this should be the same as a C compiled file in practice, yes?

jpollard 04-09-2015 01:10 PM

It should run fine interactively. If not, you can use the "-vx" option to turn on verbose messages.

The only executable (to start with) is /bin/sh, so that has to exist. One thing - depending on the location of the shell script: If it is a partition mounted with noexec, then you would get the error message you are getting.

I don't have a handy CentOS 6 system, but it should be linked to /bin/bash.

The other commands could be a problem if the PATH environment is not what the script expects, but that depends on WHEN it gets executed. A script like this would be running from rc.local.

NOTE: output to stdout will not be visible as the console terminals are not set up. Redirections to /dev/console should go to what is currently the console (and show in logs), but may also not be visible (depends on version - I think there is/will be a problem with CentOS 7, but I don't have it running at the moment either).

aavika 04-09-2015 11:58 PM

Centos 6,
i want to make a script file to read MAC address and hdd no. and size, which should be run during boot up of the linux machine.
thats y i put the file in '/etc/rc.d/init.d/info.sh' file name is 'info.sh'

i did chmod +x /etc/rc.d/init.d/info.sh
chmod 755 /etc/rc.d/init.d/info.sh

just for testing to make sure that it's working properly or givin perfect output, i want to run it manually...
after that i will check at the time of booting.

all commands of info.sh file is working perfectly.. no doubt because each and every command i checked manually by typing in linux terminal..

thanks for the reply..

veerain 04-10-2015 12:05 AM

Did you checked it's permissions after chmod +x.

Code:

ls -l /etc/rc.d/init.d/info.sh
Has it changed to executable permissions?

May be /etc is getting mounted with noexec option.

Code:

cat /proc/mounts | grep etc

aavika 04-10-2015 12:21 AM

ya.. just i checked with the command: ls -l /etc/rc.d/init.d/info.sh

output: -rwxr-xr-x 1 root root 2430 Apr 10 10:45 /etc/rc.d/init.d/info.sh

then i wrote: sh /etc/rc.d/init.d/info.sh

output: /etc/rc.d/init.d/info.sh: /etc/rc.d/init.d/info.sh: cannot execute binary file

what should i do?

pan64 04-10-2015 02:31 AM

you can try sh -xv /etc/rc.d/init.d/info.sh to check what's happening.

zeebra 04-14-2015 11:01 AM

So, in bash, when you try to just run it manually, you just type "sh scriptname.sh" with the regular user account? Or with root? Or with Sudo?

Centos should also be Systemd? Who invokes the script when?

sgosnell 04-14-2015 09:02 PM

You're trying to run it with sh, but bash is actually running it. Try changing #!bin/sh to #!bin/bash and see what happens.


All times are GMT -5. The time now is 04:05 PM.