1

I am trying to execute perl script that works with perforce, from crontab. This is my perl code:

print `/usr/local/bin/p4 filelog /projects/config.json`;

I am getting error:

Perforce client error:
    Connect to server failed; check $P4PORT.
    TCP connect to perforce:1666 failed.
    Name or service not known

The same command is working smoothly from cli. Any ideas what can i do with it?

polina-c
  • 6,245
  • 5
  • 25
  • 36

2 Answers2

4

It's because you don't have the P4CONFIG (or alternative) environment vars are set in your cron environment. You simply need to replicate the environment vars you have in your interactive shell (perhaps using set | grep ^P4 to determine what they are).

Find more details about setting env vars here.

Community
  • 1
  • 1
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I added P4PORT to the command, but still getting the same error. Here is my cron line: `46 4 * * * P4PORT=ss-5:1666 PERL5LIB=/lib /tmp/test >> /tmp/cronoutput 2>&1` – polina-c Sep 17 '16 at 00:00
  • I thought it went without saying, but you simple need to replicate the P4 env vars you have in your interactive environment (`set | grep ^P4`). There shouldn't be any guessing! – ikegami Sep 19 '16 at 00:18
1

I found the solution is to set the env vars in perl code:

    $ENV{'P4PORT'} = '...';
    $ENV{'P4CLIENT'} = '...';
    $ENV{'P4USER'} = '...';
    $ENV{'P4PASSWD'} = '...';
polina-c
  • 6,245
  • 5
  • 25
  • 36
  • Setting up these environment variables is what the existing answer instructs you to do. However. it suggests setting them in the crontab file, which is a far better solution than hardcoding config data in your script. That's a bad solution. – ikegami Jan 03 '17 at 18:02
  • @ikegami, existing solution does not explain how exactly to set them in crontab file. If you could add this information, it would help a lot. Thanks. – polina-c Jan 04 '17 at 17:44
  • If you are unable to determine how to set variables in a crontab after reading the documentation (`man 5 crontab`), feel free to ask a question about it. You can also ask the shell to set them (`VAR=val command`). – ikegami Jan 04 '17 at 18:07
  • @ikegami, I believe, the goal of stackoverflow is to provide focused and complete answers to the people's questions, even if the answers can be found in the documentation. – polina-c Jan 04 '17 at 20:59
  • Yes indeed. That's why I said you should feel free to ask that question. Actually, it's already been [asked](http://stackoverflow.com/q/2229825/589924). – ikegami Jan 04 '17 at 21:07
  • Cool. Could you please add this link to your answer to make it the best? – polina-c Jan 10 '17 at 04:37
  • @ikegami, I added the link to your post and marked it as most helpful. – polina-c Jan 11 '17 at 21:25