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.

Tim
  • 101,790
  • 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? – Rui F Ribeiro 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? – Stephen Kitt 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 01 '18 at 04:20
  • If sudo fails, then the service won’t restart at all. You could try running the job as root without sudo, I’m not sure what the benefit is of running as tim and then using sudo to run service tor reload as root anyway. – Stephen Kitt Nov 01 '18 at 14:51
  • @StephenKitt Thanks. Would running the job as root with sudo be okay too, although sudo isn't necessary? Suppose the command of the job is a shell script containing sudo. – Tim Nov 01 '18 at 15:04
  • @StephenKitt For example, if I may ask? – Tim Nov 01 '18 at 15:14
  • Actually no, sudo never asks for a password when run as root — I thought this was configurable, but it doesn’t seem to be. – Stephen Kitt Nov 01 '18 at 15:26
  • @StephenKitt: Thanks. if you were me, into which crontab file would you place a script containing sudo service tor reload; torsocks curl ipinfo.io/ip? – Tim Nov 01 '18 at 23:50

1 Answers1

-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`?

Tim
  • 101,790