4

I've just set-up one cron-job in cpanel, but although it seems to be executing the script it doesn't work as intended.

Here is the cron job command in cpanel:

/bin/sh /home/my-username/cronjobs/sedclearmalw.sh

and here is the content of the script :

#!/bin/bash
cd ../public_html/
grep -rl '_0xaae8' . | xargs sed -i 's/var\s_0xaae8.*//g'

I believe the cd command should be correct, as it needs to go back a directory and then enter public_html, however the second command seems to be the problem. I have tried running it via ssh, (bash sedclearmalw.sh) it seems like it's running for ~15 seconds but it's not doing its job , as i checked with the following command :

grep -rl '_0xaae8'

and it returns 1 file containing _0xaae8. Any help will be appreciated, it must be something simple as i know the above command in the shell script works all right when executed via ssh (not through the script).

  • Specify the absolute path to the cd in the first line. The time taken is high because you are using grep -rl and the default cron job directory could be / , and hence it is searching everywhere. – amisax Mar 27 '17 at 8:33
  • I will try that, do you maybe know why the grep command doesnt work when i execute it via script (ex. bash sedclearmalw.sh) but it works if i just use it directly via ssh – Conevski Mar 27 '17 at 8:37
  • The grep is not running in the directory that you want it to run in since cd ../public_html failed. - hence it is not finding anything. – amisax Mar 27 '17 at 8:45
  • @terdon The -ioption in sed is where the action happens. – Anthony Geoghegan Mar 27 '17 at 8:51
  • 1
    @AnthonyGeoghegan no it isn't! It just isn't POSIX, but it's also implemented on BSD sed, it's not restricted to GNU. Nah, no excuse, I just missed it :) – terdon Mar 27 '17 at 9:52
2

The problems is due to the use of a relative path. When cron runs a scheduled job, it uses the home directory of the owner as its working directory, e.g., if I schedule a job as the root user, its working directory will be /root/ (on a Cent OS system).

You should specify the absolute path in your cd command. If you’re not running any further commands in your script, you could just run it all in one line:

grep -rl '_0xaae8' /full/path/to/public_html/ | xargs sed -i 's/var\s_0xaae8.*//g'
  • 1
    Changing to full path to public_html did it, i also had a typo in my "dummy" data which i use to simulate files containing that keyword.. Thanks for the answer. – Conevski Mar 27 '17 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.