{"id":103854,"date":"2024-01-25T16:09:04","date_gmt":"2024-01-25T21:09:04","guid":{"rendered":"https:\/\/jumpcloud.com\/?post_type=support&p=103854"},"modified":"2024-01-29T11:48:29","modified_gmt":"2024-01-29T16:48:29","slug":"run-macos-commands-as-current-logged-in-user","status":"publish","type":"support","link":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user","title":{"rendered":"Run macOS Commands As Current Logged In User"},"content":{"rendered":"\n

This article covers how to utilize a template to create a macOS command that runs as a logged in user remotely via JumpCloud Commands.<\/p>\n\n\n\n

<\/p><\/div>

Important:<\/strong> \n

Only perform the following steps if you’re familiar with bash\/zsh or Shell scripting. Proceed with caution.<\/p>\n <\/div><\/div><\/div><\/div>\n\n\n\n

Understanding the Shell Script<\/h2>\n\n\n\n

The following section breaks down the major blocks within the script and what each line performs on the target device. To skip to the fully compiled script, see Compiled Script Example<\/a>. <\/p>\n\n\n\n

The first block gets the current logged in username and assigns it to the $localuser<\/em> variable:<\/p>\n\n\n\n

localuser=$(scutil <<< “show State:\/Users\/ConsoleUser” | awk ‘\/Name :\/ && ! \/loginwindow\/ { print $3 }’)<\/code><\/p>\n\n\n\n

Then you can leverage the newly created $localuser<\/em> variable by either copying the script as a file to the endpoint and calling it from the command:<\/p>\n\n\n\n

sudo -u “$localuser” \/bin\/bash “\/path\/to\/script.sh”<\/code><\/p>\n\n\n\n

or by using a block of code:<\/p>\n\n\n\n

\n

localuser=$(scutil <<< “show State:\/Users\/ConsoleUser” | awk ‘\/Name :\/ && ! \/loginwindow\/ { print $3 }’)

sudo -u “$localuser” zsh <<EOF
## begin of user scoped code block

say “This is a test of the localuser script.”
echo -e “\\a”

## end of code block
EOF<\/p>\n<\/div><\/div>\n\n\n\n

<\/p><\/div>

Note:<\/strong> \n

When you run a script that switches to user context as in the previous example, you may receive notifications from macOS Sandbox that access to the current directory is blocked.<\/p>\n\n\n\n

\u201dshell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied\u201d<\/em><\/p>\n\n\n\n

To address this, set the current working directory of the script to a non-sandboxed location with a line like cd \/Users\/Shared<\/code> at the beginning of the script.<\/p>\n <\/div><\/div><\/div><\/div>\n\n\n\n

Compiled Script Example<\/h2>\n\n\n\n

<\/p><\/div>

Important:<\/strong> \n

The following example script assumes bash or zsh shells. Ensure you have #!<\/code> line within the terminals of the target macOS device(s).<\/p>\n <\/div><\/div><\/div><\/div>\n\n\n\n

To utilize the template, insert your code to run as the logged in user between these lines: <\/p>\n\n\n\n

## begin of user scoped code block<\/code><\/p>\n\n\n\n

## end of code block <\/code><\/p>\n\n\n\n

\n

#!\/bin\/bash

cd \/Users\/Shared
sleep 1 ## if running on login event – allows ConsoleUser value to settle

localuser=$(scutil <<< “show State:\/Users\/ConsoleUser” | awk ‘\/Name :\/ && ! \/loginwindow\/ { print $3 }’)

## If no user is logged in, notify and exit gracefully.
if [ -z ${localuser} ]; then
echo “No user is currently logged in.”
exit
fi

## Execute block of code as the currently logged in user.
sudo -u “$localuser” \/bin\/bash <<EOF
## begin of user scoped code block

mkdir \/Users\/$localuser\/Applications
mkdir ~\/AnotherDir

## end of code block
EOF<\/p>\n<\/div><\/div>\n\n\n\n

Creating the JumpCloud Command<\/h2>\n\n\n\n

After you’ve verified your script using the template, you can insert the code into a JumpCloud Command to deploy it to your macOS devices remotely.<\/p>\n\n\n\n

To create the JumpCloud Command<\/strong>:<\/p>\n\n\n\n

    \n
  1. Log in to the JumpCloud Admin Portal<\/a>.<\/li>\n\n\n\n
  2. Go to DEVICE MANAGEMENT<\/strong> > Commands<\/strong>.<\/li>\n\n\n\n
  3. Click ( +<\/strong> ), then choose Command<\/strong>.<\/li>\n\n\n\n
  4. On the Details<\/strong> tab, enter a name for the new command. This is the name shown in the sortable list view of commands.<\/li>\n\n\n\n
  5. For Type<\/strong>, select Mac<\/strong>.<\/li>\n\n\n\n
  6. Under Run As <\/strong>user, select root<\/strong>.<\/li>\n\n\n\n
  7. In the Command<\/strong> field, paste the contents of the script you created using the template. <\/li>\n<\/ol>\n\n\n\n

    <\/p><\/div>

    Tip:<\/strong> \n

    For details on all available JumpCloud Commands settings, see Get Started: Commands<\/a>.<\/p>\n <\/div><\/div><\/div><\/div>\n\n\n\n

      \n
    1. Select the Device Groups<\/strong> tab to set the specific device groups where this command will execute.<\/li>\n\n\n\n
    2. Select the Devices<\/strong> tab to set the specific devices where this command will execute.<\/li>\n\n\n\n
    3. Click Save<\/strong>.<\/li>\n\n\n\n
    4. Run the command by clicking Run Now<\/strong> on the Commands page.<\/li>\n\n\n\n
    5. After the command runs, go to the Results<\/strong> tab and click View<\/strong> on the associated command to view the log. <\/li>\n<\/ol>\n\n\n\n

      <\/p>\n","protected":false},"excerpt":{"rendered":"

      This article covers how to utilize a template to create a macOS command that runs as a logged in user […]<\/p>\n","protected":false},"author":218,"featured_media":0,"template":"","meta":{"_acf_changed":false,"_oasis_is_in_workflow":0,"_oasis_original":0,"_oasis_task_priority":"","inline_featured_image":false,"footnotes":""},"support_category":[2925,2895,2852,3136,3127],"support_tag":[3160],"coauthors":[3011],"acf":[],"yoast_head":"\nRun macOS Commands As Current Logged In User - JumpCloud<\/title>\n<meta name=\"description\" content=\"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Run macOS Commands As Current Logged In User\" \/>\n<meta property=\"og:description\" content=\"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user\" \/>\n<meta property=\"og:site_name\" content=\"JumpCloud\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-29T16:48:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jumpcloud.com\/wp-content\/uploads\/2024\/07\/202405-MISC-JumpCloudHelpCenter-SiteDisplay-min-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"890\" \/>\n\t<meta property=\"og:image:height\" content=\"525\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data2\" content=\"nickconrad\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user\",\"url\":\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user\",\"name\":\"Run macOS Commands As Current Logged In User - JumpCloud\",\"isPartOf\":{\"@id\":\"https:\/\/jumpcloud.com\/#website\"},\"datePublished\":\"2024-01-25T21:09:04+00:00\",\"dateModified\":\"2024-01-29T16:48:29+00:00\",\"description\":\"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.\",\"breadcrumb\":{\"@id\":\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jumpcloud.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Support\",\"item\":\"https:\/\/jumpcloud.com\/support\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Run macOS Commands As Current Logged In User\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jumpcloud.com\/#website\",\"url\":\"https:\/\/jumpcloud.com\/\",\"name\":\"JumpCloud\",\"description\":\"Daily insights on directory services, IAM, LDAP, identity security, SSO, system management (Mac, Windows, Linux), networking, and the cloud.\",\"publisher\":{\"@id\":\"https:\/\/jumpcloud.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jumpcloud.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jumpcloud.com\/#organization\",\"name\":\"JumpCloud\",\"url\":\"https:\/\/jumpcloud.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jumpcloud.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jumpcloud.com\/wp-content\/uploads\/2021\/01\/jc-logo-brand-2021.png\",\"contentUrl\":\"https:\/\/jumpcloud.com\/wp-content\/uploads\/2021\/01\/jc-logo-brand-2021.png\",\"width\":598,\"height\":101,\"caption\":\"JumpCloud\"},\"image\":{\"@id\":\"https:\/\/jumpcloud.com\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Run macOS Commands As Current Logged In User - JumpCloud","description":"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user","og_locale":"en_US","og_type":"article","og_title":"Run macOS Commands As Current Logged In User","og_description":"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.","og_url":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user","og_site_name":"JumpCloud","article_modified_time":"2024-01-29T16:48:29+00:00","og_image":[{"width":890,"height":525,"url":"https:\/\/jumpcloud.com\/wp-content\/uploads\/2024\/07\/202405-MISC-JumpCloudHelpCenter-SiteDisplay-min-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes","Written by":"nickconrad"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user","url":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user","name":"Run macOS Commands As Current Logged In User - JumpCloud","isPartOf":{"@id":"https:\/\/jumpcloud.com\/#website"},"datePublished":"2024-01-25T21:09:04+00:00","dateModified":"2024-01-29T16:48:29+00:00","description":"Learn how to run a command as a logged-in user on macOS devices using JumpCloud Commands.","breadcrumb":{"@id":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jumpcloud.com\/support\/run-macos-commands-as-current-logged-in-user#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jumpcloud.com\/"},{"@type":"ListItem","position":2,"name":"Support","item":"https:\/\/jumpcloud.com\/support"},{"@type":"ListItem","position":3,"name":"Run macOS Commands As Current Logged In User"}]},{"@type":"WebSite","@id":"https:\/\/jumpcloud.com\/#website","url":"https:\/\/jumpcloud.com\/","name":"JumpCloud","description":"Daily insights on directory services, IAM, LDAP, identity security, SSO, system management (Mac, Windows, Linux), networking, and the cloud.","publisher":{"@id":"https:\/\/jumpcloud.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jumpcloud.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jumpcloud.com\/#organization","name":"JumpCloud","url":"https:\/\/jumpcloud.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jumpcloud.com\/#\/schema\/logo\/image\/","url":"https:\/\/jumpcloud.com\/wp-content\/uploads\/2021\/01\/jc-logo-brand-2021.png","contentUrl":"https:\/\/jumpcloud.com\/wp-content\/uploads\/2021\/01\/jc-logo-brand-2021.png","width":598,"height":101,"caption":"JumpCloud"},"image":{"@id":"https:\/\/jumpcloud.com\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support\/103854"}],"collection":[{"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support"}],"about":[{"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/types\/support"}],"author":[{"embeddable":true,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/users\/218"}],"version-history":[{"count":3,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support\/103854\/revisions"}],"predecessor-version":[{"id":104226,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support\/103854\/revisions\/104226"}],"wp:attachment":[{"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/media?parent=103854"}],"wp:term":[{"taxonomy":"support_category","embeddable":true,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support_category?post=103854"},{"taxonomy":"support_tag","embeddable":true,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/support_tag?post=103854"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/jumpcloud.com\/wp-json\/wp\/v2\/coauthors?post=103854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}