Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

[OT] Environment variable missing for scheduled task in Windows Server 2008

by Lotus1 (Vicar)
on Jun 07, 2018 at 18:20 UTC ( [id://1216125]=perlquestion: print w/replies, xml ) Need Help??

Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:

This is off topic but someone here might have seen this issue. I wrote a Perl script and set it up to run as a scheduled task with the SYSTEM user. I exported the task then imported and installed the script on some Windows 2012 servers with no problems. On a 2008 server my script complained that an environment variable is missing. I had put in a test to check for it.

if( not defined $ENV{SERVER_ENVIRONMENT} or not $ENV{SERVER_ENVIRONMEN +T} ) { print $fh_log "Environment variable \"SERVER_ENVIRONMENT\" is requ +ired to be set up. \n"; die "Environment variable \"SERVER_ENVIRONMENT\" is required to be + set up. \n"; }

The environment variable was added at the system level and I verified the spelling. I wrote this script to see what the SYSTEM user in the scheduled task has access to:

use strict; use warnings; my $logfile = 'test_env.log'; open my $fh_log, ">", $logfile or die "Couldn't open $logfile for outp +ut: $!\n"; print $fh_log "Ran at: ", scalar localtime, "\n"; print "Ran at: ", scalar localtime, "\n"; foreach (sort keys %ENV) { print $fh_log "$_ $ENV{$_}\n"; }

The environment variable is not there in the output of this script when run as a task for the SYSTEM user. The task was exported from Server 2012 and imported into 2008 so all the settings are the same. The original script runs fine from the command line. Any ideas or suggestions?

One more thing, I'm using the portable version of Strawberry Perl so the task runs as d:\portableperl\portableshell.bat d:\scripts\myscript.pl.

Replies are listed 'Best First'.
Re: [OT] Environment variable missing for scheduled task in Windows Server 2008
by Discipulus (Canon) on Jun 07, 2018 at 19:49 UTC
    Hello Lotus1,

    have you rebooted?

    No seriously: see this:

    > Changes that you make to environment variables do not affect services that run under the Local System account until you restart Windows

    L*

    PS I do not think the article is old: Last Updated: Apr 17, 2018 and given the problem I guess it affects ALL windows versions. The link about how to create system variables referes to win2000 but the underlying matter I suspect it's no changed a lot.

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      I had considered that but since the other servers didn't do this I dismissed it. But now that you mention it, there are about 10 users logged into this one and not the others. Maybe something they are doing is preventing services.exe from getting the word. Maybe it is something different about 2008. The article you posted doesn't mention what Windows version it is referring to but it has a link to setting environment variables in Windows 2000 so it looks old. Still, it is a good find. It's pretty hard to find relevant articles with words like 'system' and 'server' and 'variable' in the search terms.

      I'll try rebooting in a few minutes after everyone logs out.

      Update: I rebooted and now the System account sees all the environment variables. Thanks.

Re: [OT] Environment variable missing for scheduled task in Windows Server 2008
by trwww (Priest) on Jun 08, 2018 at 21:59 UTC
    Its been a while but I think what I did was made a .bat file that set the environment variable and started the perl script.

      Yes, that would work. Or I could have just passed the value as parameter to the script but then the scheduled task would need to be altered each time it is installed. Or the Perl script could have it hard coded but that makes updates a hassle. Or I could have a bat file to call the Perl script and that confuses people. I wrote the script for others to install and was trying to keep it simple for them. I set up the scheduled task and exported it. Then I documented how to create the environment variable and import the scheduled task, etc. Also, this variable will be useful to have available for future scripts. It worked fine for all the servers except one with no reboots needed. I'm expecting the problem to go away after rebooting since I found other problems on the server but I have to wait until Monday.

        Did the reboot work? I guess personally I prefer config files to using the environment for this stuff.

        EDIT: ah I see it did work. Good stuff

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1216125]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found