question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request: Linux] - A way to use "-noprofile" when running script directly from shell ( $> ./myscript.ps1 ) using #!/usr/bin/env pwsh

See original GitHub issue

Hey there, I apologize if there is a way to do this that I am not aware of, but I have not come across info on it if there is.

My request is to have a way to run scripts directly from a shell without loading the items in my $PROFILE.

Currently, I have a PowerShell script I created running on Linux which I use to parse /proc/net/dev to determine the current network rate. As seen in the excerpt below, the script uses the shebang #!/usr/bin/env pwsh in order to allow me to invoke the script on a schedule easily from Jobber as seen in the second dropdown below. There doesn’t seem to be a way that I am aware of to run the script without the unnecessary overhead of including the loading of my $PROFILE, such as custom prompt and other modules that are only needed/used in an active terminal.

I was attempting to profile the script by watching it in HTOP and noticed that upon each invocation it was somewhere in the neighborhood of 100MB in size and 27 threads, which I thought was a bit excessive. I went into my $PROFILE and commented everything out and right away saw each invocation dropped down to 87MB (still 27 threads). While still pretty heavy, definitely better.

I realize that there is always going to be fairly substantial overhead of running a PowerShell script on Linux compared to simply running a Bash script due to the runtime and all that, but I feel it would definitely help with adoption to be able to run things in a familiar manner without having to exclusively choose between any sort of personal customization and trying to maintain minimal overhead for additional performance in these two situations (me, actively using pwsh in a terminal vs having a script run on a schedule).

All in all, I love being able to have the much more friendly and familiar(to me, at least) coding experience of the .Net ecosystem on my Linux boxes and was hoping to utilize it more often and in different situations, but if each script is going to cost 100MB a pop, it seems that I my thought of replacing a lot of these small random bash scripts on my system with PowerShell scripts may not be the best idea and will end up being much more situational based on need. Either way, though, I really do like at least having the option and ability to do so, at least. 👍

Thanks, -MH

Script Excerpt
#!/usr/bin/env pwsh
<#	
	.NOTES
	======================================================
     Created on:   03/24/2021
     Created by:   instance.id
     Platform:     Linux
	 Filename:     traffic.ps1
	======================================================
	.DESCRIPTION
		Parses cat /proc/net/dev to determine current network transfer rate
#>

# --------------- Linux current network transfer rate check
# ---------------------------------------------------------
param (
    [Parameter()]
    [int]$Sleep = 1,
    [string]$Device,
    [switch]$Human,
    [switch]$NoLabel,
    [switch]$Pad,
    [switch]$Conky
)

Jobber Invocation
jobs:
  speed:
     cmd: /home/mosthated/.conky/scripts/traffic.ps1 -Device enp6s0 -Sleep 1 -Human -Pad -Conky
     time: '*/2 * * * * *'
     onError: Continue

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Mar 25, 2021

Nice, @237dmitry - didn’t know about the -S option for making env split the single argument it is given on Linux into individual arguments, but there’s a caveat:

  • On Linux distros, it requires GNU coreutils v8.30+, which are included with Ubuntu 19.04+, for instance, but not with Ubuntu 18.04 and earlier. In other words: older Linux distros may not support it (though that problem will take care of itself over time).

  • (On macOS it is supported since at least Catalina; it isn’t necessary there, but does no harm, which makes the solution portable.)

Just to spell out @jborean93’s suggestion:

  • Create the helper script, pwsh-np (from PowerShell) in /usr/bin (Linux) / /usr/local/bin (macOS):
@'
#!/bin/sh
pwsh -noprofile "$@"
'@ | sudo pwsh -noprofile -c { $newBin = Join-Path ($IsLinux ? '/usr/bin' : '/usr/local/bin') pwsh-np; $input > $newBin; chmod a+x $newBin }
  • Then you can use it in your shebang lines; e.g.:
#!/usr/bin/env pwsh-np

'Look, ma - no profile!'
1reaction
237dmitrycommented, Mar 25, 2021
#!/usr/bin/env -S pwsh -noprofile

script itself

and

chmod +x yourscript

to run it out from pwsh shell.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make the new, future no-profile CLI for use in shebang ...
The existing CLI cannot be fixed so as not to break backward compatibility, but it was agreed that (a) would be fixed by...
Read more >
How to run a python program from powershell based on ...
So my script is named: myscript , and the first line of the file is: #!/usr/bin/env python3 ... How can I make Windows...
Read more >
PowerShell Execute Script With NoProfile Parameter
In this quick article, I will explain how to execute script with NoProfile parameter in PowerShell. You can use NoProfile parameter of PowerShell.exe...
Read more >
Provide A Batch File To Run Your PowerShell Script From
ps1 file and choose Open With –> Windows PowerShell to run the script, it will fail with a warning saying that the execution...
Read more >
How can I get this script to run without prompt?
Solution: Usually I just call the script bypassing the executionpolicy altogether:powershell.exe -noprofile -windowstyle hidden ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found