bash - echo: write error: invalid argument

43,193

Solution 1

I think it has something to with permissions. I don't think root has write access to those files by default. Try echoing manually 'disable' to that file, even as root you get the same error shown. So to make your script work, first do chmod 744 on $i before your echo, it should do the trick.

Solution 2

I had this problem too in Docker on Alpine linux environment. I think the problem is that echo by default put a newline character at the end of the string, and the kernel not accept it, but it is not the case in every system. In Docker I had this error, but the value was written despite the error message.

The solution (in Bash): echo -n disable >/sys/firmware/acpi/interrupts/gpe66. This way no newline is echoed.

Solution 3

Double-check all spelling. echo "disabled" will emit a write error even for root whereas echo "disable" succeeds.

Share:
43,193
norman
Author by

norman

Hopefully all the stuff I asked in college is helpful to folks

Updated on July 09, 2022

Comments

  • norman
    norman almost 2 years

    I am new to bash and trying to write a script that disables kworker business as in aMaia's answer here.

    So far, I have this, which I run from root:

      1 #!/bin/bash                                                                      
      2                                                                                  
      3 cd /sys/firmware/acpi/interrupts                                                 
      4 for i in gpe[[:digit:]]* # Don't mess with gpe_all                               
      5 do                                                                               
      6     num=`awk '{print $1}' $i`                                                    
      7     if (( $num >= 1000 )); then  # potential CPU hogs?                           
      8         # Back it up and then disable it!!                                       
      9         cp $i /root/${i}.backup                                                  
     10         echo "disable" > $i                                                      
     11     fi                                                                           
     12 done  
    

    But running it results in:

    ./kkiller: line 10: echo: write error: Invalid argument
    

    What is going on here? I thought $i was just the file name, which seems like the correct syntax for echo.

    Suggestions for cleaning up/improving the script in general are also appreciated!

    Update: With set -vx added to the top of the script, here is a problematic iteration:

    + for i in 'gpe[[:digit:]]*'
    awk '{print $1}' $i
    ++ awk '{print $1}' gpe66
    + num=1024908
    + ((  1024908 >= 1000  ))
    + cp gpe66 /root/gpe66.backup
    + echo disable
    ./kkiller: line 10: echo: write error: Invalid argument
    
  • norman
    norman over 9 years
    Hmm...that still gives the same error. I had been doing it manually before with no problem, so this is definitely strange!
  • MajorT
    MajorT over 9 years
    If you can do echo disable > /sys/firmware/acpi/interrupts/gpe66 then the problem is somewhere else. But I just tried it on my system and it seems to be working.
  • norman
    norman over 9 years
    It might be a permissions thing after all, because (maybe I should've clarified in the question), by 'running it from root' I meant doing sudo -i and then the script, which led to the error. If, instead, I do sudo ./kkiller, it works like a charm. (?!)
  • MajorT
    MajorT over 9 years
    That astonishes me, by my knowledge there is no difference, since in both cases the program will execute under user root.
  • fheshwfq
    fheshwfq over 6 years
    Yes!! this worked! Kubuntu 17.10 on some kind of MacBook Pro.