小编mkl*_*nt0的帖子

在没有root权限的情况下,在Mac OS X上定期运行shell脚本

我想在不使用root的情况下在mac os x上启动.sh类型或.py文件,我在google搜索并发现launchctl可以帮助我,

所以我读教程并在教程中做同样但它不适合我,[我使用mac os x 10.9 x64]

我的.plist文件[每隔60秒运行1.sh文件]:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.alvin.crontabtest</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/paul/Desktop/1.sh</string>
  </array>
  <key>Nice</key>
  <integer>1</integer>
  <key>StartInterval</key>
  <integer>60</integer>
  <key>RunAtLoad</key>
  <true/>
  <key>StandardErrorPath</key>
  <string>/tmp/AlTest1.err</string>
  <key>StandardOutPath</key>
  <string>/tmp/AlTest1.out</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

1.sh的来源:

echo '+' >> /Users/paul/Desktop/worked.txt
Run Code Online (Sandbox Code Playgroud)

我将Run.plist放在/Users/paul/Run.plist中

并从终端运行命令:

launchctl load /Users/paul/Run.plist
Launchctl start com.alvin.crontabtest
Run Code Online (Sandbox Code Playgroud)

命令执行没有任何错误,但我没有看到working.txt中的任何内容

有人可以帮我吗?

macos bash cron startup launchd

10
推荐指数
2
解决办法
1万
查看次数

为什么以及这两个$ null值有何不同?

显然,在PowerShell(第3版)中并非所有$null都是相同的:

    >function emptyArray() { @() }
    >$l_t = @() ; $l_t.Count
0
    >$l_t1 = @(); $l_t1 -eq $null; $l_t1.count; $l_t1.gettype()
0
IsPublic IsSerial Name                                     BaseType                                                         
-------- -------- ----                                     --------                                                         
True     True     Object[]                                 System.Array                                                     
    >$l_t += $l_t1; $l_t.Count
0
    >$l_t += emptyArray; $l_t.Count
0
    >$l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
True
0
You cannot call a method on a null-valued expression.
At line:1 char:38
+ $l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
+                                      ~~~~~~~~~~~~~~~
  + CategoryInfo …
Run Code Online (Sandbox Code Playgroud)

powershell null automation-null

10
推荐指数
2
解决办法
738
查看次数

Powershell命令修剪路径,如果它以"\"结束

如果它结束,我需要修剪路径\.

C:\Ravi\
Run Code Online (Sandbox Code Playgroud)

我需要换到

C:\Ravi
Run Code Online (Sandbox Code Playgroud)

我有一个路径不会结束的情况\(然后它必须跳过).

我尝试过.EndsWith("\"),但是当我有\\而不是时,它失败了\.

这可以在PowerShell中完成,而无需诉诸条件吗?

powershell text-parsing filepath ends-with

10
推荐指数
2
解决办法
8616
查看次数

PowerShell中的构造函数链接 - 在同一个类中调用其他构造函数

我正在做一些测试,偶然发现了以下情况:

您可以根据需要重载PoShv5中的方法.如果调用不带参数的方法,它可以在内部使用参数调用方法,以保持代码不冗余.我预计这对于构造函数也是如此.

在此示例中,最后一个构造函数按预期工作.其他构造函数仅返回没有设置值的对象.

Class car {
    [string]$make
    [string]$model
    [int]$Speed
    [int]$Year

    speedUp (){
        $this.speedUp(5)
    }
    speedUp ([int]$velocity){
        $this.speed += $velocity
    }

    # Constructor
    car () {
        [car]::new('mall', $Null, $null)
    }

    car ([string]$make, [string]$model) {
        [car]::new($make, $model, 2017)
    }

    car ([string]$make, [string]$model, [int]$Year) { 
        $this.make = $make
        $this.model = $model
        $this.Year = $year
    }
}

[car]::new() # returns "empty" car
[car]::new('Make', 'Nice model') # returns also an "empty" one
[car]::new( 'make', 'nice model', 2017) # returns a "filled" instance
Run Code Online (Sandbox Code Playgroud)

有没有办法来解决这个问题?我错过了什么?

powershell constructor overloading class constructor-chaining

10
推荐指数
2
解决办法
1715
查看次数

任何相当于Kotlin的延期

Coroutine async返回Deferred<T>,有懒惰执行和等待用法的例子.

但是,我们如何等待任何一个Deffered实例完成?

简而言之

  // whats the equivalent of CompletableFuture.anyOf(...)?
  // is this how we do it? if so how costly is this?
  select<Unit> {
     deffered1.onAwait {}
     deffered2.onAwait {}
  }
Run Code Online (Sandbox Code Playgroud)

coroutine kotlin completable-future

10
推荐指数
1
解决办法
453
查看次数

用cat读取:不接收数据时停止

有没有办法告诉cat命令在没有收到任何数据时停止阅读?可能有一些"超时",指定没有数据传入多长时间.

有任何想法吗?

unix linux shell sh cat

9
推荐指数
2
解决办法
5037
查看次数

在文件资源管理器中打开目录

在ms上,从node.js代码开始,如何c:\documents在Windows文件浏览器中打开特定目录(例如:)?

我想在c#中,它会是:

process.Start(@"c:\test")
Run Code Online (Sandbox Code Playgroud)

windows node.js

9
推荐指数
2
解决办法
8804
查看次数

Powershell将环境变量与路径连接起来

所以我有这个代码:

$userprofile=Get-ChildItem Env:USERPROFILE
$localpath="$userprofile\some\path"
Run Code Online (Sandbox Code Playgroud)

我希望下面的输出来自$localpath:

c:\users\username\some\path
Run Code Online (Sandbox Code Playgroud)

但是我得到的是:

System.Collections.DictionaryEntry\some\path
Run Code Online (Sandbox Code Playgroud)

所以,当然,像cd $localpath失败的东西.我如何实现我的需求?

string environment-variables filepath powershell-4.0

9
推荐指数
1
解决办法
7639
查看次数

范围vs Runspace vs Session vs AppDomain

我正在努力为PowerShell划清界限.我的理解非常有限:

  • 范围包含用户/脚本定义的变量和函数,并且可以存在具有PS调用堆栈的范围层次结构.
  • Runspace规定了给定PS实例可以访问的内置功能.Runspace可以跨越网络边界.
  • Session是Powershell的特定实例.这些也可以跨越网络边界.
  • 应用程序域(或AppDomain)包含已加载的程序集.在许多情况下,一旦将数据加载到AppDomain中,就无法卸载它.必须处理AppDomain以支持新的AppDomain.如果通过其他应用程序调用PS脚本,则创建的PS实例将继承调用应用程序的AppDomain.

谁能更好地解释这些概念?是否有某种维恩图或其他能够充实这些信息的东西?在线文档并没有太大帮助.

powershell session appdomain runspace

9
推荐指数
1
解决办法
576
查看次数

通过 Powershell 关闭时无法停止所有 Excel 进程

使用此代码,我打开 excel(使用visible = false,以便用户看不到它),写入工作簿,然后在脚本结束后打开 excel(使其可见)或完全关闭它而不保存。当我保存 Excel、使其保持打开状态、结束脚本,然后稍后手动关闭 Excel 时,任务管理器中没有后台进程。但是,当我使用脚本关闭 Excel 时,它仍保留在任务管理器中。

以下是我开始使用 Excel 的方法:

    $script:excel = new-object -ComObject excel.application # create excel object
    $excel.visible = $false # hide excel window
    $script:workbook = $excel.Workbooks.Add() # add excel file
    $script:ws1 = $workbook.Worksheets.Item(1) # create new sheet
Run Code Online (Sandbox Code Playgroud)

这是我关闭它的方法:

        [gc]::Collect()
        [gc]::WaitForPendingFinalizers()
        if ($script:closeOnX) {
            #only do this if not keeping excel open
            Write-Host "Closing Excel"
            $excel.Quit()
        }
        [System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel)
Run Code Online (Sandbox Code Playgroud)

closeOnX 只是一个标志,因此它仅在某些情况下实际关闭 Excel 应用程序。其余部分在每次脚本结束时执行。

当我结束脚本并同时关闭 Excel 时,我只想关闭当前的 Excel 进程(这就是我不想停止进程的原因),而不关闭用户可能正在处理的其他工作簿。

当我结束脚本、保存并打开 Excel 时,我希望当用户手动关闭 Excel 时所有进程都消失。(这是工作)

com excel powershell user-interface rcw

9
推荐指数
1
解决办法
4252
查看次数