0

According to https://unix.stackexchange.com/a/478636/674, cron modifies the execution environment to execute a job.

  1. When I run sudo service tor reload twice directly in bash, it always shows two different IP addresses

    $ sudo service tor reload; torsocks  curl ipinfo.io/ip; sudo service tor reload; torsocks  curl ipinfo.io/ip
    12.345.678.901
    987.654.321.00
    

    Is it correct that sudo service tor reload will most likely change tor exit node, and thus change the public IP address?

  2. But when I create a cron job in /etc/cron.d/myjob,

    * * * * * tim (sudo service tor reload; torsocks  curl ipinfo.io/ip; sudo service tor reload; torsocks  curl ipinfo.io/ip) > /tmp/cron.log
    

    every time I check /tmp/cron.log, it always shows two identical IP addresses, although the IP address changes from job to job (when the next scheduled job overwrites the log, it will show two identical IP addresses not identical to the two before overwriting). Same when I insert sleep 20 between the two reload:

    * * * * * tim (sudo service tor reload; torsocks  curl ipinfo.io/ip; sleep 20; sudo service tor reload; torsocks  curl ipinfo.io/ip) > /tmp/cron.log
    

    As a cron job, why does sudo service tor reload fail to change the IP address? How can I make it work?

Thanks.

10
  • Could it be a coincidence? Could it be one reload has not finished? Could it be that both reloads are not being done? Am I making any sense? Oct 31 '18 at 13:55
  • It happens consistently. Try it yourself, and you will see.
    – Tim
    Oct 31 '18 at 13:56
  • If the IP addresses change every time you run the cron job, isn’t it doing its job (from your perspective), even though the two runs inside a cron job don’t? Oct 31 '18 at 13:57
  • @StephenKitt You are correct. That should do my job. I repeat it twice in one cron job, just to let me check the overwritten log to make sure the IP changes. But it doesn't change within one job, and surprises me.
    – Tim
    Oct 31 '18 at 13:58
  • @StephenKitt Can it be because in the crontab file, I specify a non-root user tim to run the cron job, while the cron job contains sudo? Will the sudo command fail because there is no way to provide the root password?
    – Tim
    Nov 1 '18 at 4:20
-1

The cause of the problem is: in the job line, the command uses sudo, while the user field is a nonroot user tim. I don't provide tim's password asked for by sudo.

I am still trying to understand what the best practice of writing the user and command fields of a cron job. Are cron tasks in `/etc/cron.d/` files supposed to not contain `sudo`?

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.