Udev won't execute some lines in my script

0

I'm working on a Beaglebone Black under debian. I'm writing Udev rules to manage µSD card. What i need Udev to do: when my µSD is plugged in, Mount the µSD on /media/sdcard (it does) and execute the script that does:

  • Stop rsyslog.service (it does)
  • Move /var/log to /media/sdcard (it does NOT)
  • create a link from /var/log to /media/sdcard/log (to continue logging but in the SDcard) (it does NOT but the /media/sdcard/log does not exist because the precedent step has failed)
  • restart rsyslog service (it does)

Here is the script that does what i just described:

logger 'Enterring /root/ServolineScripts/LogsonSDcard.sh script'
logger 'Refreshing Mountinglogs'
sudo rm /var/log/Mountinglogs.log
logger 'Stopping syslog service'
sudo /etc/init.d/rsyslog stop
sudo mv /var/log /media/sdcard/
sudo ln -s /media/sdcard/log /var/log
sudo /etc/init.d/rsyslog start
logger 'Successfully restarted rsyslog, logs are back on syslog!'
logger 'Restarted syslog service'
logger 'Quitting /root/ServolineScripts/LogsonSDcard.sh script'

I'm not sure why it does not move the /var/log directory. This same script executed by hand with a simple sh /root/ServolineScripts/LogsonSDcard.sh works...

I wonder if it is a problem of rights...

JeromeLEKIEFFRE

Posted 2017-11-16T15:37:29.393

Reputation: 1

Gosh, why there are much sudo calls? udev runs its scripts with root permissions, it does not need no sudo. And sudo may easily not do what you supposedly expect it to in the general case. I mean, please stop mindlessly replicating whatever crap you see in random blog posts; put a hefty amount of thought in what you implement. – kostix – 2017-11-17T07:01:23.073

OK, I digress. Now to your real problem. If you remove all those sudo prefixes, does it work? Another possible approach is to have a wrapper script which would run another one — containing only the "action" statements and no calls to logger and pipe both its output streams to the logger (which is able to consume input on its stdout). Something like script_which_does_stuff 2>&1 | logger -t microsd – kostix – 2017-11-17T07:05:29.193

you're right with the overusage of sudo ;) I did not reflect that Udev was executing as root... (stupid boy!) OK i already had the idea to do an actions script and i was wondering why it should work... but i'm on it! Thanks! i'll be back when it will work (i hope...) – JeromeLEKIEFFRE – 2017-11-17T07:56:54.077

i'm back and it works much better without the sudo! ;) And with two seperate scripts. @kostix do you have an explanation on why it works with two different scripts and not with one? – JeromeLEKIEFFRE – 2017-11-17T08:56:31.133

I don't. But I think that leaving out sudo could be the sole reason it works ;-) – kostix – 2017-11-17T09:21:25.577

i just have one more strange thing... i did a copy and paste in different functions that i call in my main script but when the script creates the link from /var/log to /media/sdcard/log the result is a link in /media/sdcard/log to /media/sdcard/log... very strange because executed by hand the first script i wrote in my post was working perfectly... – JeromeLEKIEFFRE – 2017-11-17T09:47:42.440

Answers

0

I write an answer with what helped me in the comments.

Credits to @kostix ;)


The main problem was probably the overusage of sudo in a script that is executed by Udev which already have root permissions. @kostix proposed me to write two different scripts, one with the actions and one main that calls the functions in the actions script. I did both of the solution, deleting the sudo that don't need to exist and creating an other script with actions. It worked perfectly and i'm now able to look on other problems in my script!

JeromeLEKIEFFRE

Posted 2017-11-16T15:37:29.393

Reputation: 1